Using bit manipulation


  • 0
    S

    def firstMissingPositive(self,nums):

        if not nums:
            return 1
    
        n=max(nums)
        res=0
        for each in nums:
            if each>0:
                val=1<<each
                if (res&val)==0:
                    res^=val
        
        for each in range(1,n+1):
            val=1<<each
            if (val&res)==0:
                return each
    
        return n+1

Log in to reply
 

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