# 缺失的第一个正数

<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>

以下程序实现了这一功能，请你填补空白处内容：

```python
class Solution(object):
	def firstMissingPositive(self, nums):
		ls = len(nums)
		index = 0
		while index < ls:
			if nums[index] <= 0 or nums[index] > ls or nums[nums[index] - 1] == nums[index]:
				index += 1
			else:
				pos = nums[index] - 1
				nums[index], nums[pos] = nums[pos], nums[index]
		res = 0
		_________________________;
		return res + 1
# %%
s = Solution()
print(s.firstMissingPositive(nums = [1,2,0]))
```

## template

```python
class Solution(object):
	def firstMissingPositive(self, nums):
		ls = len(nums)
		index = 0
		while index < ls:
			if nums[index] <= 0 or nums[index] > ls or nums[nums[index] - 1] == nums[index]:
				index += 1
			else:
				pos = nums[index] - 1
				nums[index], nums[pos] = nums[pos], nums[index]
		res = 0
		while res < ls and nums[res] == res + 1:
			res += 1
		return res + 1
# %%
s = Solution()
print(s.firstMissingPositive(nums = [1,2,0]))
```

## 答案

```python
while res < ls and nums[res] == res + 1:
	res += 1
```

## 选项

### A

```python
while res < ls and nums[res] == res:
	res += 1
```

### B

```python
while res <= ls and nums[res] == res:
	res += 1
```

### C

```python
while res > ls and nums[res] == res + 1:
	res += 1
```