LeetCode 137. Single Number II

来源:互联网 发布:纵向科研经费知乎 编辑:程序博客网 时间:2024/05/16 01:30

描述

找出数组中给出的只出现了一次的数

解决


class Solution {public:    int singleNumber(vector<int>& nums) {        int one = 0, two = 0, three = 0;        for (auto it : nums)        {            //求出现两次的bit位            two |= (one & it);            //出现一次的bit位            one ^= it;            //printf("%x\n", one);            //printf("%x\n", two);            //若出现了三次,则把这些bit置为0,求出哪些为需要置为0            three = ~(one & two);            //出现了三次的bit都要置为0            one &= three;            two &= three;        }        return one;    }};
0 0
原创粉丝点击