My java solution, O(n)


  • 0
    E
      public int missingNumber(int[] nums) {
            int length = nums.length;
            int sum = 0;
            int acc = 0;
            for(int k=0;k<length;k++){
                sum += nums[k];
                acc += k+1;
            }
            if(sum ==acc)return 0;
            else 
            return acc - sum;
        }
    

Log in to reply
 

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