41. First Missing Positive

 

 

Given an unsorted integer array, find the smallest missing positive integer.

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

Note:

Your algorithm should run in O(n) time and uses constant extra space.

 

 

 

 

 

http://www.cnblogs.com/grandyang/p/4395963.html

 

https://leetcode.com/problems/first-missing-positive/discuss/17071/My-short-c++-solution-O(1)-space-and-O(n)-time

 

Put each number in its right place.

 

For example:

 

When we find 5, then swap it with A[4].

 

At last, the first place where its number is not right, return the place + 1.

 

 

这个

https://www.youtube.com/watch?v=cG1rZPIo3ww

posted on 2018-11-08 16:56  猪猪🐷  阅读(101)  评论(0)    收藏  举报

导航