zoukankan      html  css  js  c++  java
  • leetcode-hard-array-41. First Missing Positive-NO

    mycode

    class Solution(object):
        def firstMissingPositive(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if not nums:
                return 1
            nums = sorted(nums)
            max_n = max(nums)
            for i in range(1,max_n+1):
                if i not in nums:
                    return i
            return max_n + 1
    Runtime Error Message:Line 11: MemoryError
    Last executed input:[2147483647]
     
    44.76%
    class Solution(object):
        def firstMissingPositive(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if not nums:
                return 1
            if 1 not in nums:
                return 1
            else:
                nums = sorted(set(nums))
                pos = nums.index(1)
                if pos == len(nums) -1:
                    return 2
                else:
                    nums[:] = nums[pos:]
                    for i in range(1,len(nums)):
                        if not i+1 == nums[i]:
                            return i + 1
                    return len(nums) + 1

    参考

    class Solution(object):
        def firstMissingPositive(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            l = range(0,-len(nums)-1,-1)   [0,-1,-2,-3...]
            print(l)
            if not len(nums):
                return 1
            
            for n in nums:
                if n < len(l) and n > 0:
                    l[n] = n
                                    
            for n in l:
                if n < 0:
                    return -n
                
            return len(l)
                    
  • 相关阅读:
    WebApi下载附件文件
    zabbix
    redis主从与集群搭建
    redis
    mariadb集群配置(主从和多主)
    使用python执行sql语句和外键解析
    mariadb(四)连接查询,视图,事物,索引,外键
    mariadb(三)查
    mariadb(二)增删改
    firewalld
  • 原文地址:https://www.cnblogs.com/rosyYY/p/11039108.html
Copyright © 2011-2022 走看看