Leetcode.Solved.Missing-number

// https://leetcode.com/problems/missing-number
public class Solution {
    public int MissingNumber(int[] nums) {
                    
    int[] arr = new int[nums.Length + 1];

    for (int i = 0; i < nums.Length; i++) {
       arr[nums[i]] = 1; 
    }
    
    for(int i=0; i< arr.Length; i++)
    {
      if(arr[i] == 0)        
        return i;
        
    }
    
      return 0;    
  }
}
// this decision doesn't work well because of unclear conditions
// https://leetcode.com/problems/missing-number
public class Solution {
    

    public int MissingNumber(int[] nums) {
        
		// to add many cases here, not submitting        
		 
        int min = nums[0];
        int max = nums[0];
        var set = new HashSet<int>();
        for (int i = 0; i < nums.Length; i++) {
            if (nums[i] < min) {
                min = nums[i];
            }

            if (nums[i] > max) {
                max = nums[i];
            }                        

            set.Add(nums[i]);
        }
         
        for (int num = min; num <= max; num++) {
          if (!set.Contains(num))
            return num;
        }

        return ++max; // not clear from conditions of the task        
    }
}
This entry was posted in Без рубрики. Bookmark the permalink.