Re: [闲聊] 每日LeetCode

楼主: JerryChungYC (JerryChung)   2024-01-16 02:52:46
※ 引述《sustainer123 (caster )》之铭言:
: ※ 引述《yam276 (史莱哲林的优等生)》之铭言:
: : 思路:
: : 1. 先把题目给的阵列分成赢跟输的HashMap
: : 2. 寻找有在赢Map没在输Map的 = Never_loses
: : 3. 寻找输Map数字是1的 = 一败仔
: : 4. Sort 因为不是照顺序
: Python code:
: class Solution:
: def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
: mc = {}
: pm = {}
: for e in matches:
: if e[0] in mc:
: mc[e[0]] +=1
: else:
: mc[e[0]] = 1
: if e[1] in pm:
: pm[e[1]] +=1
: else:
: pm[e[1]] = 1
: no_losses = sorted([key for key in mc.keys() if key not in pm])
: one_losses = sorted([key for key,value in pm.items() if value == 1])
: return [no_losses,one_losses]
原本用2个list会炸掉 又想不到其他解法只好来偷看
再修改了一些地方
Python3 code:
class Solution:
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
win = {}
lose = {}
for match in matches:
win[match[0]] = 1
lose[match[1]] = 2 if match[1] in lose else 1
winners = sorted([k for k in win if k not in lose])
once = sorted([k for k, v in lose.items() if v == 1])
return [winners, once]
win的value是多少不影响 所以直接给1
lose只需要=1的 所以重复就直接给2

Links booklink

Contact Us: admin [ a t ] ucptt.com