剑指 Offer II 004. 只出现一次的数字

2022-04-06  本文已影响0人  邦_

首先想到的 用一个字典记录出现的次数
如果到达3次 就从字典中移除 最后剩下的就是没有重复的

func singleNumber(_ nums: [Int]) -> Int {

        var dict  = Dictionary<Int, Int>()

        for n in nums {
            
            if let m =  dict[n]  {
                
                dict[n] = m + 1
                if dict[n] == 3 {
                    dict.removeValue(forKey: n)
                }
                
            }else {
                
                dict[n] = 1
            }
                        
        }
        
        return dict.keys.first ?? 0

    }

位运算的没看懂。。暂缺。。

上一篇 下一篇

猜你喜欢

热点阅读