Efficient C++ Solution


  • 0
    F

    This code improves the best case solution. No need to loop through till the end except for the last missing number. Also, less no. of XOR operations are needed in this solution. However, sorting is used as numbers may not be sorted in the given vector.

    int missingNumber(vector<int>& nums) {
          sort(nums.begin(),nums.end());
          int i=0;
          for(i=0;i<nums.size();i++)
              if((nums[i] ^ i) !=0)break;          
          return i;
        }
    

Log in to reply
 

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