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?

思路

真大佬在这,感觉智商被碾压
https://www.nowcoder.com/questionTerminal/1097ca585245418ea2efd0e8b4d9eb7a

public class Solution {
    public int singleNumber(int[] a) {
        if(a==null)return 0;
        int ones = 0;
        int twos = 0;
        int threes;
        for (int i = 0; i < a.length; i++) {
            int t = a[i];
            twos |= ones&t;
            ones ^= t;
            threes = ones&twos;
            ones &= ~threes;
            twos &= ~threes;
        }
        return ones;
    }
}