[LeetCode] 030: First Missing Positive
来源:互联网 发布:js隐藏菜单栏 编辑:程序博客网 时间:2024/06/17 20:17
[Problem]
[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 {说明:版权所有,转载请注明出处。Coder007的博客
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;
}
};
阅读全文
0 0
- [LeetCode] 030: First Missing Positive
- LeetCode: First Missing Positive
- LeetCode First Missing Positive
- LeetCode: First Missing Positive
- [Leetcode] First Missing Positive
- [LeetCode] First Missing Positive
- [Leetcode] First Missing Positive
- leetcode First Missing Positive
- LeetCode First Missing Positive
- [leetcode] first missing positive
- LeetCode: First Missing Positive
- [LeetCode]First Missing Positive
- LeetCode-First Missing Positive
- [leetcode] First Missing Positive
- [LeetCode] First Missing Positive
- LeetCode - First Missing Positive
- LeetCode | First Missing Positive
- Leetcode: First Missing Positive
- [LeetCode] 027: Distinct Subsequences
- Activity的那些事
- [LeetCode] 028: Divide Two Integers
- [LeetCode] 029: Edit Distance
- asynctask_httpclient
- [LeetCode] 030: First Missing Positive
- Makefile---$@ $^ $<
- [LeetCode] 031: Flatten Binary Tree to Linked List
- c#修改程序名称——转载
- [转载] Catalan数及其应用
- [LeetCode] 032: Generate Parentheses
- [LeetCode] 033: Gray Code
- Python基础(8)-软件开发规范
- [LeetCode] 034: Implement strStr()