Java AC code, may help u!


  • 0
    Z
    public class Solution {
        //bit manipulation
        public int missingNumber(int[] nums){
            int[] bits = new int [32];
            for(int i = 0; i < nums.length; i++){
                int number = i + 1;
                for(int j = 0; j < 32; j++){
                    bits[j] ^= number & 1;
                    bits[j] ^= nums[i] & 1;
                    number >>= 1;
                    nums[i] >>= 1;
                }
            }
            int result = 0;
            int tmp = 1;
            for(int i = 0; i < 32; i++){
                result += bits[i] == 1 ? tmp : 0;
                tmp <<= 1;
            }
            return result;
        }
        //normal solution
        /*public int missingNumber(int[] nums) {
            int total = nums.length;
            for(int i = 0; i < nums.length; i++)
                total += i - nums[i];
            return total;
        }*/
    }

Log in to reply
 

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