题目:
Given an unsorted integer array, find the smallest missing positive integer.
Example 1:
Input: [1,2,0] Output: 3
Example 2:
Input: [3,4,-1,1] Output: 2
Example 3:
Input: [7,8,9,11,12] Output: 1
题目不是很难,就是不容易AC
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
if(n == 0) return 1;
sort(nums.begin(),nums.end());
int i;
int first = 0;
for(i = 0; i < n; i++)
{
if(nums[i] <= 0)
continue;
if(!first)
{
if(nums[i] > 1)
return 1;
first = 1;
continue;
}
if(nums[i] - nums[i - 1] > 1)
{
return nums[i - 1] + 1;
}
}
if(nums[n - 1] >= 0) /* [-5] */
return nums[n - 1] + 1;
else
return 1;
}
};