an easy understood method using hashtable


  • 0
    J

    import java.util.Hashtable;
    public class Solution {
    public int firstMissingPositive(int[] nums) {

        Hashtable<Integer,Integer> hash = new Hashtable();
        for(int i = 0;i < nums.length;i++){
            hash.put(nums[i],1);
        }
    
        for(int i = 1;i <= nums.length+1;i++){
            if(!hash.containsKey(i))
                return i;
        }
        return 1;
    }
    

    }


Log in to reply
 

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