https://leetcode.com/problems/first-missing-positive/

来源:互联网 发布:linux date 格式化 编辑:程序博客网 时间:2024/06/05 10:26

https://leetcode.com/problems/first-missing-positive/

题目很好懂 

我的做法是

1.删除所有非正数 

2.去重

3.排序

4.看下标和值不相差1 如果都相同 那就少list里的下一个

一定有更好的方法 

class Solution:    # @param {integer[]} nums    # @return {integer}    def firstMissingPositive(self, nums):        listi=[]        for i in nums:            if i>0:                listi.append(i)        listi = list(set(listi))#去重的函数 !!!!!!        listi.sort()#排序        if len(listi)==0:            return 1        else:            for i in range(len(listi)):                if listi[i]!=i+1:                    return i+1                    break                else:                    if i==len(listi)-1:                        return len(listi)+1            


0 0
原创粉丝点击