# 缺失的第一个正数

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

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

```java
class Solution {
	public int firstMissingPositive(int[] nums) {
		int n = nums.length;
		int contains = 0;
		for (int i = 0; i < n; i++) {
			if (nums[i] == 1) {
				contains++;
				break;
			}
		}
		if (contains == 0) {
			return 1;
		}
		for (int i = 0; i < n; i++) {
			_________________;
		}
		for (int i = 0; i < n; i++) {
			int a = Math.abs(nums[i]);
			nums[a - 1] = -Math.abs(nums[a - 1]);
		}
		for (int i = 0; i < n; i++) {
			if (nums[i] > 0) {
				return i + 1;
			}
		}
		return n + 1;
	}
}
```

## template

```java
class Solution {
	public int firstMissingPositive(int[] nums) {
		int n = nums.length;
		int contains = 0;
		for (int i = 0; i < n; i++) {
			if (nums[i] == 1) {
				contains++;
				break;
			}
		}
		if (contains == 0) {
			return 1;
		}
		for (int i = 0; i < n; i++) {
			if ((nums[i] <= 0) || (nums[i] > n)) {
				nums[i] = 1;
			}
		}
		for (int i = 0; i < n; i++) {
			int a = Math.abs(nums[i]);
			nums[a - 1] = -Math.abs(nums[a - 1]);
		}
		for (int i = 0; i < n; i++) {
			if (nums[i] > 0) {
				return i + 1;
			}
		}
		return n + 1;
	}
}
```

## 答案

```java
if ((nums[i] <= 0) || (nums[i] > n)) {
	nums[i] = 1;
}
```

## 选项

### A

```java
if (nums[i] <= 0) {
	nums[i] = 1;
}
```

### B

```java
if ((nums[i] <= 0) || (nums[i] > n)) {
	nums[i] = 0;
}
```

### C

```java
if (nums[i] > n) {
	nums[i] = 1;
}
```