javascript solution


  • 0
    W
    /**
     * @param {number[]} nums
     * @return {number}
     */
    var firstMissingPositive = function(nums) {
        var missing = {};
        var found = {};
        nums.forEach((k, i) => {
            if (!found[i + 1]) {
                missing[i + 1] = true;
            } 
            if (k > 0) {
                if (!found[k + 1]) {
                    missing[k + 1] = true;
                }
                found[k] = true; // deal
                delete missing[k];
            }
        });
        return +Object.keys(missing)[0] || 1;
    };
    

Log in to reply
 

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