Really simple (5 LOC) Java solution using Bitset


  • -2
    J
    public class Solution {
        
        public int firstMissingPositive(int[] nums) {
            BitSet bitset = new BitSet();    
            for (Integer i : nums) {
                if (i > 0) {
                    bitset.set(i);
                }
            }
            
            return bitset.nextClearBit(1);
        }
    }

  • 0
    G

    This problem requires constant space


Log in to reply
 

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