題目描述如下:
分析如下:
此題目乍一看挺難的沒有思路,其實很簡單,他要找消失的兩個數字,那麼也及時其實原數組應該是在現有輸入數組的基礎上加上兩個數字,此時range()函數就可以上場了,但是很多人說,你這樣會有重複哎,咋辦呢,直接使用set()去重複即可。
代碼如下:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 """ 4 # @Time : 2020/7/13 10:23 5 6 # @Author : ZFJ 7 8 # @File : 消失的兩個數字.py 9 10 # @Software: PyCharm 11 """ 12 13 14 class Solution(object): 15 def missingTwo(self, nums): 16 """ 17 :type nums: List[int] 18 :rtype: List[int] 19 """ 20 result = list(set(range(1, len(nums) + 3)) - set(nums)) 21 return result 22 23 24 if __name__ == "__main__": 25 test = Solution().missingTwo(nums=[1]) 26 test2 = Solution().missingTwo(nums=[2, 3]) 27 print(f"第一組中缺失的兩個數字是:{test}") 28 print(f"第二組中缺失的兩個數字是:{test2}")
運行消耗如下: