###### LeetCode First Missing Positive

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

java code:

public class Solution {
public int firstMissingPositive(int[] nums) {
if(nums == null || nums.length == 0) return 1;

for(int i = 0; i < nums.length; ++i) {
while(nums[i] > 0 && nums[i] <= nums.length && nums[i] != i + 1) {
if(nums[nums[i] - 1] == nums[i]) break;
swap(nums, i, nums[i] - 1);
}
}
for(int i = 0; i < nums.length; ++i) {
if(nums[i] != i + 1) return i + 1;
}
return nums.length + 1;
}

void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

#### 【LeetCode】First Missing Positive 解题报告

2014-12-28 21:00:14

#### LeetCode(41) First Missing Positive

2014-12-31 08:08:19

#### Leetcode：First Missing Positive

2013-10-23 16:27:01

#### First Missing Positive -- LeetCode

2014-03-13 04:31:27

#### Leetcode First Missing Positive 三个思路

2013-12-06 07:52:46

#### leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

2015-07-10 10:25:58

#### leetcode_c++：First Missing Positive（041）

2016-05-23 00:28:48

#### LeetCode | First Missing Positive（第一个缺失的正整数）

2014-08-10 19:35:52

#### First Missing Positive （寻找第一个丢失的正数）【leetcode】

2013-09-18 16:19:01

#### leetcode笔记：First Missing Positive

2015-12-24 01:45:52

## 不良信息举报

LeetCode First Missing Positive