2001. 可互换矩形的组数
哈希计数 然后 在累加 C x 2 C_x^2 Cx2
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
dic = defaultdict(int)
for (a,b)in rectangles:
t = a/b
dic[t] +=1
sum = 0
for i in dic:
sum +=(dic[i])*(dic[i]-1)//2
#print(dic)
return sum
383. 赎金信
哈希 先加 后减 出现负1 直接反弹
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
dic =defaultdict(int)
for s in magazine:
dic[s] +=1
for s in ransomNote:
dic[s] -=1
if dic[s] < 0:
return False
return True