First Missing Positive
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0]
return 3
,
and [3,4,-1,1]
return 2
.
Your algorithm should run in O(n) time and uses constant space.
Title: Looking for the first missing integer meaning "1,2,4,5,6,7" then lost is 3, if "1,1000", then lost is 2
Note that additional space is required in O (n) time and is not allowed
So the title is need you in the range from 1 to +∞, find the smallest normal class solution { public) that did not appear in the input array. int firstmissingpositive (int[] nums) { int len=nums.lengt h; if(nums==null| | Len==0)
return1; ///First determine whether the current element is an integer, otherwise directly over, and Num[j]<len will not cause the array out of bounds, and finally judge if NUMS[J]-1!=[J] prevent the cycle of death for(intj=0;j<len;j++){ while(nums[j]>0&&nums[j]<=len&& (nums[j]-1)! =j) { intTemp=nums[nums[j]-1]; if(temp==Nums[j]) { Break; } Nums[nums[j]-1]=Nums[j]; NUMS[J]=temp; } } for(intj=0;j<len;j++){ if(nums[j]!=j+1){ returnJ+1; } }
returnlen+1; }}
First Missing Positive "Leetcode" Looking for a missing integer, Java, algorithm