Re: [閒聊] 每日LeetCode

看板Marginalman作者 (JerryChung)時間4月前 (2024/01/22 13:22), 編輯推噓2(201)
留言3則, 2人參與, 4月前最新討論串616/719 (看更多)
https://leetcode.com/problems/set-mismatch 645. Set Mismatch 有一組1~n的整數set,但有一個數字重複與一個數字丟失了,找出重複與缺失的數回傳。 Example 1: Input: nums = [1,2,2,4] Output: [2,3] Example 2: Input: num = [1,1] Output: [1,2] 思路: 1. 做一個1~n組成的set,利用差集找出缺失的數 2. 用Counter找出出現2次的數 Python3 code: -------------------------------------- class Solution: def findErrorNums(self, nums: List[int]) -> List[int]: range_list = set(range(1, len(nums) + 1)) counter = Counter(nums) return [[_ for _ in counter if counter[_] == 2][0], list(range_list - set(nums))[0]] 後來看其他人的答案,可以用數學計算總合相減得到結果 前兩天的有想法但都會遇到TLE 好麻煩 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.251.103 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1705900935.A.FCF.html

01/22 13:50, 4月前 , 1F
大師
01/22 13:50, 1F

01/22 13:50, 4月前 , 2F
前兩天是哪題阿
01/22 13:50, 2F

01/22 13:55, 4月前 , 3F
動態規劃兩題ㄅ
01/22 13:55, 3F
文章代碼(AID): #1bhVk7_F (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bhVk7_F (Marginalman)