leetcode——448.找到所有数组中消失的数字

class Solution:
    def findDisappearedNumbers(self, nums) -> int:
        a=set(nums)
        b=len(nums)
        c=[]
        while b>0:
            if b not in a:
                c.append(b)
                b-=1
            else:
                b=b-1
        return c
执行用时 :536 ms, 在所有 Python3 提交中击败了59.58%的用户
内存消耗 :23.5 MB, 在所有 Python3 提交中击败了11.39%的用户
 
执行用时为 148 ms 的范例
class Solution:
    def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
        nums_set = set(nums)
        ret = []
        for i in range(1,len(nums) + 1):
            if i not in nums_set:
                ret.append(i)
        return ret
执行用时 :448 ms, 在所有 Python3 提交中击败了79.58%的用户
内存消耗 :23.8 MB, 在所有 Python3 提交中击败了5.06%的用户
【不知道为啥用时不同】
思路相同。。。
                                                                                                               ——2019.10.9
 
 

猜你喜欢

转载自www.cnblogs.com/taoyuxin/p/11643959.html