From e1bbd30d8ec02cab5b1c11dfb360f9252c809382 Mon Sep 17 00:00:00 2001 From: borninfreedom Date: Tue, 15 Jun 2021 16:01:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?update=20=E5=9B=9B=E6=95=B0=E7=9B=B8?= =?UTF-8?q?=E5=8A=A0II=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=9B=B4=E5=8A=A0=E7=AE=80=E6=B4=81=E7=9A=84python?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0454.四数相加II.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index 835178dd..0621ab5b 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -120,7 +120,7 @@ class Solution { Python: -``` +```python class Solution(object): def fourSumCount(self, nums1, nums2, nums3, nums4): """ @@ -147,7 +147,31 @@ class Solution(object): if key in hashmap: count += hashmap[key] return count + +# 下面这个写法更为简洁,但是表达的是同样的算法 +# class Solution: +# def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int: +# from collections import defaultdict + +# hashmap = defaultdict(int) + +# for x1 in nums1: +# for x2 in nums2: +# hashmap[x1+x2] += 1 + +# count=0 +# for x3 in nums3: +# for x4 in nums4: +# key = -x3-x4 +# value = hashmap.get(key) + + # dict的get方法会返回None(key不存在)或者key对应的value + # 所以如果value==0,就会继续执行or,count+0,否则就会直接加value + # 这样就不用去写if判断了 + +# count += value or 0 +# return count ``` From 999bbae8c24ed0dae872603ea0c0aab0ae1d7afe Mon Sep 17 00:00:00 2001 From: borninfreedom Date: Tue, 15 Jun 2021 16:13:23 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=B0=86python3=E7=9A=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=8C=BA=E6=A0=87=E8=AF=86=E6=94=B9=E4=B8=BApython=EF=BC=8C?= =?UTF-8?q?=E5=9B=A0=E4=B8=BAtypora=E4=B8=8D=E8=83=BD=E8=AF=86=E5=88=ABpyt?= =?UTF-8?q?hon3=EF=BC=8C=E5=90=8C=E6=97=B6=E6=94=B9=E4=B8=BApython?= =?UTF-8?q?=E5=AF=B9=E9=AB=98=E4=BA=AE=E9=98=85=E8=AF=BB=E5=B9=B6=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=BD=B1=E5=93=8D=EF=BC=8C=E8=80=8C=E4=B8=94python?= =?UTF-8?q?=E5=AF=B9=E5=85=B6=E4=BB=96markdown=E8=BD=AF=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=A7=E6=9B=B4=E5=A5=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 2 +- problems/0349.两个数组的交集.md | 2 +- problems/0707.设计链表.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 02e9996f..31a808b0 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -107,7 +107,7 @@ public int[] twoSum(int[] nums, int target) { Python: -```python3 +```python class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: hashmap={} diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 090480a4..5c635d39 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -118,7 +118,7 @@ class Solution { ``` Python: -```python3 +```python class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: result_set = set() diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 2fa1af29..2d096063 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -239,7 +239,7 @@ class MyLinkedList { ``` Python: -```python3 +```python # 单链表 class Node: From fb21a5727e249ce000d14edf59b291b6d099ce2b Mon Sep 17 00:00:00 2001 From: borninfreedom Date: Tue, 15 Jun 2021 16:50:32 +0800 Subject: [PATCH 3/3] =?UTF-8?q?update=E8=B5=8E=E9=87=91=E4=BF=A1=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=BD=BF=E7=94=A8=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E5=93=88=E5=B8=8C=E8=A1=A8=E5=92=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8defaultdict=E4=B8=A4=E7=A7=8D=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 49 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 23e2c5fd..5374a08f 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -135,8 +135,52 @@ class Solution { ``` -Python: -```py +Python写法一(使用数组作为哈希表): + +```python +class Solution: + def canConstruct(self, ransomNote: str, magazine: str) -> bool: + + arr = [0] * 26 + + for x in magazine: + arr[ord(x) - ord('a')] += 1 + + for x in ransomNote: + if arr[ord(x) - ord('a')] == 0: + return False + else: + arr[ord(x) - ord('a')] -= 1 + + return True +``` + +Python写法二(使用defaultdict): + +```python +class Solution: + def canConstruct(self, ransomNote: str, magazine: str) -> bool: + + from collections import defaultdict + + hashmap = defaultdict(int) + + for x in magazine: + hashmap[x] += 1 + + for x in ransomNote: + value = hashmap.get(x) + if value is None or value == 0: + return False + else: + hashmap[x] -= 1 + + return True +``` + +Python写法三: + +```python class Solution(object): def canConstruct(self, ransomNote, magazine): """ @@ -166,6 +210,7 @@ class Solution(object): ``` Go: + ```go func canConstruct(ransomNote string, magazine string) bool { record := make([]int, 26)