# 缺失的第一个正数

<p>给你一个未排序的整数数组 <code>nums</code> ，请你找出其中没有出现的最小的正整数。</p><p> </p><p><strong>进阶：</strong>你可以实现时间复杂度为 <code>O(n)</code> 并且只使用常数级别额外空间的解决方案吗？</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>nums = [1,2,0]<strong><br />输出：</strong>3</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>nums = [3,4,-1,1]<strong><br />输出：</strong>2</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>nums = [7,8,9,11,12]<strong><br />输出：</strong>1</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>0 <= nums.length <= 300</code></li>	<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li></ul>
<p>以下<font color="red">错误</font>的选项是？</p>

## aop

### before

```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after

```cpp
int main()
{
    Solution sol;
    vector<int> nums = {1, 2, 0};
    int res = 8;

    res = sol.firstMissingPositive(nums);

    cout << res;
    return 0;
}
```

## 答案

```cpp
class Solution
{
public:
    int firstMissingPositive(vector<int> &nums)
    {
        int n = nums.size();
        for (int i = 0; i < n; ++i)
        {
            while (nums[i] > 0 && nums[i] < n && nums[nums[i] - 1] != nums[i])
            {
                swap(nums[i], nums[nums[i] - 1]);
            }
        }
        for (int i = 0; i < n; ++i)
        {
            if (nums[i] != i + 1)
                break;
        }
        return n + 1;
    }
};
```
## 选项


### A

```cpp
class Solution
{
public:
    int firstMissingPositive(vector<int> &nums)
    {
        if (nums.size() == 0)
        {
            return 1;
        }
        int i = 0;
        while (i < nums.size())
        {
            if (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.size() && nums[nums[i] - 1] != nums[i])
            {
                swap(nums[i], nums[nums[i] - 1]);
            }
            else
            {
                i++;
            }
        }
        for (i = 0; i < nums.size(); i++)
        {
            if (nums[i] != i + 1)
            {
                break;
            }
        }
        return i + 1;
    }
};
```

### B

```cpp
class Solution
{
public:
    int firstMissingPositive(vector<int> &nums)
    {
        int i, k, n = nums.size();
        for (i = 0; i < n; i++)
        {
            while (nums[i] > 0 && nums[i] <= n)
            {
                k = nums[i] - 1;
                if (nums[k] == nums[i])
                    break;
                swap(nums[i], nums[k]);
            }
        }
        for (i = 0; i < n; i++)
        {
            if (i + 1 != nums[i])
                break;
        }
        return i + 1;
    }
};
```

### C

```cpp
class Solution
{
public:
    int firstMissingPositive(vector<int> &nums)
    {
        int n = nums.size();
        for (int i = 0; i < n; i++)
        {
            if (nums[i] <= 0 || nums[i] > n || (nums[nums[i] - 1] == nums[i]))
                continue;
            swap(nums[nums[i] - 1], nums[i]);
            i--;
        }
        for (int i = 0; i < n; i++)
        {
            if (nums[i] != i + 1)
                return i + 1;
        }
        return n + 1;
    }
};
```
