LeetCode41. Missing first positive number

topic

Given an unsorted integer array, please find the smallest positive integer that does not appear in it. link

Ideas

  • Ordinary method, set directly (/dog)
class Solution {
    
    
    public int firstMissingPositive(int[] nums) {
    
    
        Set<Integer>set = new HashSet<>();
        int res = 1;
        for(int num : nums){
    
    
            set.add(num);
            if(set.contains(res)){
    
    
                res++;
            }
        }
        while(set.contains(res)){
    
    
                res++;
            }
        return res;
    }
}

Borrowing the explanation from the big guys in the comment area
hashing in place is equivalent to getting every number nback to n-1the home of the subscript .


Those who didn't return home became lone ghosts and wandering away. They either had no home at all ( nums[i] <= 0 || nums[i] > len), or their home was occupied by others (repetition appeared).


These homeless people were temporarily placed in the iempty houses marked as below. The reason why the houses are available is because the owner of house i is i+1missing (the number is i+1missing).
Therefore, by constructing the hash in place and letting each number go home, we can find the repeated numbers and the missing numbers in the original array.

First determine that the range of the result is [1, len + 1]between, len为数组长度when the element in the array does not have 1, the result is 1, and when the element in the array covers [1, len]all the numbers in, the result is len + 1.


Therefore, the array subscript can be associated with the elements in the array. When in nums[i]∈[1, len]this interval, the corresponding relationship is nums[i] = i + 1that no operation is performed when it does not belong to this interval. After traversing the array in [1, len]this way, the number in this interval covered by the array can be determined by the subscript .


Traverse the array again, and add one to i + 1the index of the first element that does not satisfy the corresponding relationship , and the result is len + 1. Otherwise, the result is .

class Solution {
    
    
    public int firstMissingPositive(int[] nums) {
    
    
        int len = nums.length;
        for (int i = 0; i < len; i++) {
    
    
        	//这里用while是因为nums[i]交换过后的值可能仍然不满足nums[i] = i + 1
        	//所以需要继续交换,直到nums[i]不在[1,len]这个区间
            while (nums[i] >= 1 && nums[i] <= len && nums[nums[i] - 1] != nums[i]) {
    
    
                swap(nums, i, nums[i] - 1);
            }
        }
        //遍历找到第一个不满足对应关系的元素
        for (int i = 0; i < len; i++) {
    
    
            if (nums[i] != i + 1) {
    
    
                return i + 1;
            }
        }
        return len + 1;
    }

    private void swap(int[] nums, int i, int i1) {
    
    
        int temp = nums[i];
        nums[i] = nums[i1];
        nums[i1] = temp;
    }
}

Guess you like

Origin blog.csdn.net/qq_42007742/article/details/106979949