Solution
-
The result should be maximally
n
, the length ofnums
. -
We can use
nums
to keep track of which number is missing. - For any appeared
num
, setnums[num]
toNone
.Ignore all negative and num >
n
. - The first index of not
None
is the result.
def firstMissingPositive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
idx = 0
l = len(nums)
while idx < l:
num = nums[idx]
if num is None or num <= 0 or num > len(nums):
idx += 1
continue
tmp = nums[num-1]
nums[num-1] = None
if idx != num-1 and tmp is not None:
nums[idx] = tmp
else:
idx += 1
for i, num in enumerate(nums):
if num is None:
continue
else:
return i + 1
return l + 1
Solution - Improvement
nums[nums[i]%n]+=n
: It stores information about nums[i] and still keep the information of nums[nums[i]].mod
: original valuediv
: if the index has appeared
def firstMissingPositive(self, nums):
nums.append(0)
n = len(nums)
for i in range(len(nums)): #delete those useless elements
if nums[i]<0 or nums[i]>=n:
nums[i]=0
for i in range(len(nums)): #use the index as the hash to record the frequency of each number
nums[nums[i]%n]+=n
for i in range(1,len(nums)):
if nums[i]/n==0:
return i
return n
NEXTJump Game II