leetcode: First Missing Positive
首先先拿出最强方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21class Solution(object):
def firstMissingPositive(self, nums):
L = 0
R = len(nums)
while L < R:
if nums[L] == L + 1:
L += 1
elif nums[L] > R or nums[L] <= 0 or nums[L] == nums[nums[L]-1]:
R = R - 1
nums[L], nums[R] = nums[R], nums[L]
else:
tmp = nums[L] - 1
nums[L], nums[tmp] = nums[tmp], nums[L]
return L + 1
# 测试用例
s = Solution()
print s.firstMissingPositive([3,4,-1,1])
print s.firstMissingPositive([2,1])
print s.firstMissingPositive([2,2])