今天做了道题,试了下Python,简单粗暴
Easy First Missing Positive
28%
Accepted
Given an unsorted integer array, find the first missing positive integer.
Example
Given [1,2,0]
return 3
, and [3,4,-1,1]
return 2
.
Challenge
Tags
Expand
Your algorithm should run in O(n) time and uses constant space.
代码:
class Solution:
# @param A, a list of integers
# @return an integer
def firstMissingPositive(self, A):
# write your code here
result = 1
if len(A) == 0:
return result
else:
for key in sorted(A):
if key == result:
result = result+1
return result