How can I improve upon the run time, please review


  • 0
    M
    public class Solution {
        public int missingNumber(int[] nums) {
            Map<Integer, Boolean> myMap = new HashMap<Integer, Boolean>();
            int max=0;
            if(nums.length==0){
                return -1;
            }
            if(nums.length==1){
                if(nums[0] != 0){
                    return 0;
                }
            }
            for(int i=0;i<nums.length;i++){
                myMap.put(nums[i], true);
                if(nums[i]>max){
                    max=nums[i];
                }
            }
            for(int i=0;i<nums.length;i++){
                if(!myMap.containsKey(i)){
                    return i;
                }
            }
            return max+1;
        }
    }

Log in to reply
 

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