From 5055e15cd9773ccfa036f8782e70aa00d86732e4 Mon Sep 17 00:00:00 2001 From: shuwen Date: Fri, 13 Aug 2021 10:45:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200383.=E8=B5=8E=E9=87=91?= =?UTF-8?q?=E4=BF=A1.md=20Python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用collections.Counter实现 --- problems/0383.赎金信.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index f2c1685a..d4b73848 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -209,6 +209,22 @@ class Solution(object): return True ``` +Python写法四: + +```python3 +class Solution: + def canConstruct(self, ransomNote: str, magazine: str) -> bool: + c1 = collections.Counter(ransomNote) + c2 = collections.Counter(magazine) + x = c1 - c2 + #x只保留值大于0的符号,当c1里面的符号个数小于c2时,不会被保留 + #所以x只保留下了,magazine不能表达的 + if(len(x)==0): + return True + else: + return False +``` + Go: ```go