[LeetCode] 030: First Missing Positive
2017-09-10 20:48
453 查看
[Problem]
Given an unsorted integer array, find the first missing positive integer.
For example,
Given
and
Your algorithm should run in O(n) time and uses constant space.
[Solution]
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.
[Solution]
class Solution { public: int firstMissingPositive(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function for(int i = 0; i < n; ++i){ if(A[i] <= 0 || A[i] == i+1)continue; int val = A[i], tmpV; while(val > 0 && val <= n && val != A[val-1]){ tmpV = A[val-1]; A[val-1] = val; val = tmpV; } } // find for(int i = 0; i < n; ++i){ if(A[i] != i+1){ return i+1; } } return n+1; } };说明:版权所有,转载请注明出处。Coder007的博客
相关文章推荐
- [leetcode]First Missing Positive
- leetcode - First Missing Positive
- [LeetCode]First Missing Positive(Java)
- LeetCode41 - First Missing Positive
- LeetCode_First Missing Positive
- First Missing Positive leetcode java
- leetCode (41)--First Missing Positive
- [Leetcode] First Missing Positive
- LeetCode First Missing Positive
- LeetCode: First Missing Positive 解题报告
- *LeetCode-First Missing Positive
- [Leetcode] First Missing Positive (Java)
- [LeetCode29]First Missing Positive
- [LeetCode] 找到第一个不存在的正整数 First Missing Positive
- [LeetCode]题解(python):041-First Missing Positive
- LeetCode First Missing Positive
- leetcode之first missing positive
- leetcode first missing positive,覆盖区间
- Leetcode: First Missing Positive
- First Missing Positive -- LeetCode