Clean Java Code


  • 0
    A
    public int singleNumber(int[] nums) {
    	int result = 0;
    	int bit = 0;
    	for (int i = 0; i < 32; i++) {
    		bit = 0;
    		for (int n : nums)
    			bit = bit + ((n >> i) & 1);
    		result = result << 1;
    		result += bit % 3;
    	}
    	return reverseBits(result);
    }
    
    public int reverseBits(int n) {
    	if (n == 0)
    		return 0;
    	int result = 0;
    	for (int i = 0; i < 32; i++) {
    		result = result << 1;
    		if ((n & 1) == 1)
    			result++;
    		n = n >> 1;
    	}
    	return result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.