solution.md 3.3 KB
Newer Older
1 2
# 缺失的第一个正数
<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>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
3
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
4

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
## 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;
    }
};
```