No math solution by swapping elements into right position


  • 0
    Y
    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            for(int i = 0; i < nums.size(); )
            {
                if(nums[i] == nums.size()||nums[i] == i)  
                {
                    i++;
                    continue;
                }
                swap(nums[i], nums[nums[i]]);
            }
            for(int i = 0; i < nums.size(); i++)
            {
                if(nums[i] != i)
                {
                    return i;
                }
            }
            return nums.size();
        }
    };

Log in to reply
 

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