leetcode subject 41. The first positive number missing

topic

Given an unsorted array of integers, find the smallest positive integer which does not arise.

Examples

Example 1:

Input: [1,2,0]
Output: 3

Example 2:

Input: [3,4, -1,1]
Output: 2

Example 3:

Input: [7,8,9,11,12]
Output: 1

A thought

A total of n numbers, we want to find the smallest positive integer not appear before. When the pile is the number 1 to n, achieved the maximum result of n + 1, if there are duplicate numbers, then the number n accounts for less than 1 to n, n is greater than the number we can no matter what.
So we open the array of n + 2, index = 0 we do not use the location, index = 1 to n + 1 we save the results, first of all these arrays are set to the initial value 0, through the array, there have been few, we corresponding array index value is set to 1. Finally iterate to find the first array element is 0, and returns the index.

Thinking a Code

public class problem41 {
	public int firstMissingPositive(int[] nums) {
        if(nums.length==0) return 1;
        int flag[]=new int[nums.length+2];//标记数组
        for(int i=0;i<nums.length;i++){
            if(nums[i]>0&&nums[i]<nums.length+2){
                flag[nums[i]]=1;//出现过的数进行标计
            }
        }
        for(int i=1;i<flag.length;i++){
            if(flag[i]==0) return i;//找出第一个未出现过的正整数
        }
        return 0;
    }
	public static void main(String[] args) {
		problem41 pro=new problem41();
		int test[]={3,4,-1,1};
		System.out.println(pro.firstMissingPositive(test));
	}
}

Thinking two (do not consume extra space)

We use 1-n cycle times, we determined whether the first value are set to 1, if there have been no 1 from start to finish, a direct return on the line 1-n, and the range is not appeared. Second cycle so we examine all the elements, this element as the index value, the corresponding value becomes negative, represents the value of this index represents occurred (e.g., an index value of 2 -5 in the array, represents 2 array appeared to indicate when the actual writing code is 2 + 1, because of the border issue). The last cycle, find out wood promising a positive number, there is a direct return index value, no return n + 1;

Thinking second code

public class problem41_2 {
	public int firstMissingPositive(int[] nums) {
		boolean flag=false;
		int size=nums.length;
		
		//对数组元素预处理
		for(int i=0;i<nums.length;i++){
			//判断是否出现过1
			if(nums[i]==1) flag=true;
			if(nums[i]<=0||nums[i]>size) nums[i]=1;
		}
		
		//未出现过1,直接返回
		if(!flag) return 1;
		
		//遍历记录元素,类似bitmap的思想,正数表示此索引代表值未出现,负数代表出现
		for(int i=0;i<nums.length;i++){
			nums[Math.abs(nums[i])-1]=-Math.abs(nums[Math.abs(nums[i])-1]);
		}
		
		//再次遍历,判断是否还有正数
		for(int i=0;i<nums.length;i++){
			if(nums[i]>0) return i+1;
		}
			
        return size+1;
    }
	public static void main(String[] args) {
		problem41_2 pro=new problem41_2();
		int test[]={1,2,0};
		System.out.println(pro.firstMissingPositive(test));
	}
}

Published 15 original articles · won praise 0 · Views 174

Guess you like

Origin blog.csdn.net/qq_36360463/article/details/104040610