Easy java solution


  • 0
    C
    public int missingNumber(int[] nums) {
            int res = 0;
            Arrays.sort(nums);
            if(nums.length == 0 || nums[0] != 0) return res;
            
            for(int i = 1; i < nums.length; i ++) {
                if((nums[i] - 1) != nums[i - 1]) {
                    res = i;
                    return res;
                }
                res++;
            }
            return res + 1;
        }

  • 0
    J

    what if the input array is {4,5,7}, then, return 0?


  • 0
    M

    sorting array doesn't make this algorithm linear time complexity.


Log in to reply
 

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