Single Number II 每个数出现3次,求单个数

来源:互联网 发布:ubuntu 启动mysql服务 编辑:程序博客网 时间:2024/06/06 03:23

Single Number II

 

Given an array of integers, every element appears three times except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

class Solution {public://出现3次,每位都3次,对3取余为0,,剩余的就是那个单独的数    int singleNumber(vector<int>& nums) {                int one=0,two=0,three=0;        for(int i=0;i<nums.size();i++)        {            two|=one&nums[i];            one^=nums[i];            three=(one&two);            one&= ~three;//三次清0            two&= ~three;        }        return one;    }};

0 0