That's a simple MATH problem


  • 1
    S

    I think we can take this problem as a MATH problem, the sum should be (n+0)*(n+1)/2, then minus each of the element in the array.

    public class Solution {
        public int missingNumber(int[] nums) {
            int n = nums.length;
            int sum = n*(n + 1)/2;
            for(int i : nums)
                sum-= i;
            return sum;
        }
    }

  • 0
    Y

    what if the sum overflows?


  • 0
    C

    yes. it may overflow. you must use bit operation or use string adder


Log in to reply
 

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