【剑指offer】【位运算】56 - II. 数组中数字出现的次数 II

位运算 + 状态转移

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

猜你喜欢

转载自www.cnblogs.com/Trevo/p/12760250.html
今日推荐