First Missing Positive "Leetcode" Looking for a missing integer, Java, algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.