My black magic solution, with only N times of XOR!


  • 0
    K
    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            int x = 0;
            for(int num: nums)  x = x ^ num;
            switch(nums.size()&3) {
                case 2:
                    return x^1^nums.size();
                case 0:
                    return x^nums.size();
                case 1:
                    return x^1;
                case 3:
                    return x;
            }
        }
    };
    

Log in to reply
 

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