码迷,mamicode.com
首页 > 其他好文 > 详细

Single Number II

时间:2015-08-21 19:14:02      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

其实就是设置三个标志位,出现一次标志位1对应的bit变为1,出现两次标志位2对应的bit变为1,出现三次标志位三对应的bit变为1.

理解了这种思路,代码也就不难写了。

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int one=0;
        int two=0;
        int three=0;
        for(int i=0;i<nums.size();i++)
        {
            two|=one&nums[i];
            one^=nums[i];
            three=one&two;
            one&=~three;
            two&=~three;
        }
        return one|two;
    }
};

  

Single Number II

标签:

原文地址:http://www.cnblogs.com/qiaozhoulin/p/4748559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!