Leetcode 41 Solution
This article provides solution to leetcode question 41 (first-missing-positive).
Access this page by simply typing in "lcs 41" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/first-missing-positive
Solution
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
for (int i = 0; i < nums.size(); i++)
{
while ((nums[i] > 0 && nums[i] <= nums.size()) && i + 1 != nums[i] && nums[i] != nums[nums[i] - 1])
swap(nums[i], nums[nums[i] - 1]);
}
for (int i = 0; i < nums.size(); i++)
if (nums[i] != i + 1)
return i + 1;
return nums.size() + 1;
}
};