```
//first missing positive
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
int i = 0;
while (i < n) {
if ( (nums[i] < 1 || nums[i] > n) || nums[i] == i + 1 || nums[i] == nums[nums[i] - 1] ) {
i++; continue;
}
//else swap nums[i] and nums[nums[i]-1]
swap(nums[i], nums[nums[i] - 1]);
}
for (i = 0; i < n; i++) {
if (nums[i] != i + 1)
return i + 1;
}
return n + 1;
}
void swap(int& a, int& b) {
int tmp = a;
a = b;
b = tmp;
}
};
```