Skip to content

Commit 46dbf5d

Browse files
committed
Feb 1
1 parent 8ad1b8f commit 46dbf5d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

41-FirstMissingPositive.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int firstMissingPositive(vector<int>& nums) {
4+
int len = nums.size();
5+
if(len == 0) return 1;
6+
7+
sort(nums.begin(), nums.end());
8+
9+
int answer = 0;
10+
for(int i = 0; i < len; i++){
11+
if(nums[i] < 0 || nums[i] == answer) continue;
12+
if(nums[i] == answer+1) answer = nums[i];
13+
else return answer+1;
14+
}
15+
16+
return (nums[len-1] < 0)? 1: nums[len-1]+1;
17+
}
18+
};

0 commit comments

Comments
 (0)