First Missing Positive


  • 0
    L

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


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.