First Missing Positive

2018/1/18 posted in  leetcode

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        unordered_map<int, int> map;
        int min = INT_MAX, max = 0, res = 1;
        for (int i = 0; i<nums.size(); i++) {
            if (nums[i]<=min&&nums[i]>=0) {
                min = nums[i];
            }else if (nums[i]<0) min = 0;
            if (nums[i]>=max) max = nums[i];
            map[nums[i]]++;
        }
        for (int j=min; j<=max; j++) {
            if (j==0) continue;
            if (min>=2) {
                return 1;
            }
            if (map[j]==0) {
                res = j;
                break;
            }
            if (j==max) {
                res = max+1;
            }
        }
        return res;
    }
};