LeetCode-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?

public class Solution
{
     
    public int singleNumber(int []nums)
    {
        int ones = 0;
        int twos = 0;
        int threes = 0;
        for(int num : nums)
        {
            twos = twos | (num&ones);
            ones = ones ^ num;
            threes = ones & twos;
            ones = ones & (~threes);
            twos = twos & (~threes);
        }
        int res = ones | twos;
        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42146769/article/details/88565866
今日推荐