Java: simple solution using HashSet


  • -1
    H
    public int firstMissingPositive(int[] nums) {
    	if(nums.length < 1) return 1;
    	Set<Integer> set = new HashSet<>();
    	for(int number : nums) if(number > 0) set.add(number);
    	if(set.isEmpty()) return 1;
    	int min = Integer.MAX_VALUE;
    	set.add(0);
    	for(int number : set) if(!set.contains(number + 1)) min = number < min ? number : min;
        return min + 1;
    }

Log in to reply
 

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