class Solution {
public:
int firstMissingPositive(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int i = 0;
while (i < n) {
if (A[i] >= 1 && A[i] <= n && A[i] != i + 1 && A[A[i] - 1] != A[i]) {
swap(A[i], A[A[i] - 1]);
}
else {
i++;
}
}
for (int i = 0; i < n; i++) {
if (A[i] != i + 1) {
return i + 1;
}
}
return n + 1;
}
};
LeetCode First Missing Positive
最新推荐文章于 2019-02-20 22:00:41 发布