Is there a constant space solution without modifying the original array?


  • 0
    C

    "Your algorithm should run in O(n) time and uses constant space."

    Is a constant space solution without modifying the original array?


  • 0
    P

    Here's my python solution with Bitwise operation

    class Solution:
    def firstMissingPositive(self, nums):
        has = 0
        for num in nums:
            if num > 0:
               has |= 1 << num
        for i in range(1,len(nums) + 1):
            has >>= 1
            if has % 2 == 0:
                return i
        return len(nums) + 1
    

Log in to reply
 

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