My simple C++ solution, 3ms.


  • 0
    S

    Using sort().So maybe not a O(n) solutions...

    class Solution {
    public:
    int firstMissingPositive(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    int res = 1;
    for (int i=0; i<nums.size(); i++){
    if (nums[i] == res)
    res++;
    else if (nums[i]>res)
    break;
    }
    return res;
    }
    };

    Can someone tell me how to put my code in right way? QAQ


Log in to reply
 

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