My C++ solution O(n) time Beats 97.88%


  • 0
    H
    class Solution {
    public:
        int firstMissingPositive(vector<int>& nums) 
        {
            unordered_map<int,int> mp;
            for(int i=0;i<nums.size();i++)
                if(nums[i]>0)
                    mp[nums[i]]=1;
            int result=1;
            for(int i=1;i<=nums.size()+1;i++)
                if(!mp[i])
                {
                    result=i;
                    break;
                }
            return result;
        }
    };
    

  • 1
    Y

    It is not constant space.


Log in to reply
 

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