今天做了道题,试了下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
使用Python解决缺失正数问题
本文介绍如何利用Python简洁高效地解决寻找未排序整数数组中第一个缺失的正数问题,提供了一种O(n)时间复杂度和常数空间的解决方案。
871

被折叠的 条评论
为什么被折叠?



