From e54ba10fa12016132f40a2d27b128270104147db Mon Sep 17 00:00:00 2001 From: Camille0512 Date: Thu, 24 Feb 2022 00:36:23 +0800 Subject: [PATCH 1/7] Added in one more python solution. Using defaultdict. --- problems/0454.四数相加II.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index a7c903eb..a6cd413b 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -141,7 +141,24 @@ class Solution(object): ``` +```python +class Solution: + def fourSumCount(self, nums1: list, nums2: list, nums3: list, nums4: list) -> int: + from collections import defaultdict # You may use normal dict instead. + rec, cnt = defaultdict(lambda : 0), 0 + # To store the summary of all the possible combinations of nums1 & nums2, together with their frequencies. + for i in nums1: + for j in nums2: + rec[i+j] += 1 + # To add up the frequencies if the corresponding value occurs in the dictionary + for i in nums3: + for j in nums4: + cnt += rec.get(-(i+j), 0) # No matched key, return 0. + return cnt +``` + Go: + ```go func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int { m := make(map[int]int) From 5b78393ac4e70db5cf9518090f4cb563c68e11d6 Mon Sep 17 00:00:00 2001 From: Camille0512 Date: Fri, 25 Feb 2022 00:57:43 +0800 Subject: [PATCH 2/7] modified 15. Add a new version with the same idea. --- problems/0015.三数之和.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index e191eabc..1d811b76 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -243,7 +243,34 @@ class Solution: right -= 1 return ans ``` +Python (v2): + +```python +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + if len(nums) < 3: return [] + nums, res = sorted(nums), [] + for i in range(len(nums) - 2): + cur, l, r = nums[i], i + 1, len(nums) - 1 + if res != [] and res[-1][0] == cur: continue # Drop duplicates for the first time. + + while l < r: + if cur + nums[l] + nums[r] == 0: + res.append([cur, nums[l], nums[r]]) + # Drop duplicates for the second time in interation of l & r. Only used when target situation occurs, because that is the reason for dropping duplicates. + while l < r - 1 and nums[l] == nums[l + 1]: + l += 1 + while r > l + 1 and nums[r] == nums[r - 1]: + r -= 1 + if cur + nums[l] + nums[r] > 0: + r -= 1 + else: + l += 1 + return res +``` + Go: + ```Go func threeSum(nums []int)[][]int{ sort.Ints(nums) From 4980496c05aad48ea28cc0ed471cc08ffe36b9d6 Mon Sep 17 00:00:00 2001 From: Camille0512 Date: Fri, 25 Feb 2022 18:59:56 +0800 Subject: [PATCH 3/7] 0001. Add python v2. Making use of the dictionary property. --- problems/0001.两数之和.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 22b2e7eb..9571a773 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -118,6 +118,18 @@ class Solution: return [records[target - val], idx] # 如果存在就返回字典记录索引和当前索引 ``` +Python (v2): + +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + rec = {} + for i in range(len(nums)): + rest = target - nums[i] + # Use get to get the index of the data, making use of one of the dictionary properties. + if rec.get(rest, None) is not None: return [rec[rest], i] + rec[nums[i]] = i +``` Go: From 0738423c9ed3979fa649ba7a147c4c60ecdec616 Mon Sep 17 00:00:00 2001 From: Camille0512 Date: Sun, 27 Feb 2022 00:24:13 +0800 Subject: [PATCH 4/7] modified 541. Can be more pythonic. --- problems/0541.反转字符串II.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index 14b8601a..cd9bb0cc 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -204,8 +204,23 @@ class Solution: return ''.join(res) ``` +Python3 (v2): + +```python +class Solution: + def reverseStr(self, s: str, k: int) -> str: + # Two pointers. Another is inside the loop. + p = 0 + while p < len(s): + p2 = p + k + # Written in this could be more pythonic. + s = s[:p] + s[p: p2][::-1] + s[p2:] + p = p + 2 * k + return s +``` Go: + ```go func reverseStr(s string, k int) string { ss := []byte(s) From f862ebf0c70c37c62f83b5ad50b441c830f5850f Mon Sep 17 00:00:00 2001 From: Camille0512 Date: Sun, 27 Feb 2022 00:41:18 +0800 Subject: [PATCH 5/7] modified offer05. Add two python versions. --- problems/剑指Offer05.替换空格.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index 530545fb..59f74a18 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -260,8 +260,24 @@ class Solution: ``` +```python +class Solution: + def replaceSpace(self, s: str) -> str: + # method 1 - Very rude + return "%20".join(s.split(" ")) + + # method 2 - Reverse the s when counting in for loop, then update from the end. + n = len(s) + for e, i in enumerate(s[::-1]): + print(i, e) + if i == " ": + s = s[: n - (e + 1)] + "%20" + s[n - e:] + print("") + return s +``` javaScript: + ```js /** * @param {string} s From 0fbacd5d9b4c72db15f5f1ddae2c73b320a8c554 Mon Sep 17 00:00:00 2001 From: Camille0512 Date: Sun, 27 Feb 2022 01:19:55 +0800 Subject: [PATCH 6/7] modified 151. Add 2 python versions. No need to implement Carlo's idea in such a complecated way by inviting extra space. --- problems/0151.翻转字符串里的单词.md | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index ead5fa12..7588cbd6 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -438,6 +438,38 @@ class Solution: ``` +```python +class Solution: + def reverseWords(self, s: str) -> str: + # method 1 - Rude but work & efficient method. + s_list = [i for i in s.split(" ") if len(i) > 0] + return " ".join(s_list[::-1]) + + # method 2 - Carlo's idea + def trim_head_tail_space(ss: str): + p = 0 + while p < len(ss) and ss[p] == " ": + p += 1 + return ss[p:] + + # Trim the head and tail space + s = trim_head_tail_space(s) + s = trim_head_tail_space(s[::-1])[::-1] + + pf, ps, s = 0, 0, s[::-1] # Reverse the string. + while pf < len(s): + if s[pf] == " ": + # Will not excede. Because we have clean the tail space. + if s[pf] == s[pf + 1]: + s = s[:pf] + s[pf + 1:] + continue + else: + s = s[:ps] + s[ps: pf][::-1] + s[pf:] + ps, pf = pf + 1, pf + 2 + else: + pf += 1 + return s[:ps] + s[ps:][::-1] # Must do the last step, because the last word is omit though the pointers are on the correct positions, +``` Go: From 6e3f394893c39edae8864fffc71eee986e81361f Mon Sep 17 00:00:00 2001 From: Camille0512 Date: Mon, 7 Mar 2022 00:33:30 +0800 Subject: [PATCH 7/7] Add level order traversal --- problems/0101.对称二叉树.md | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index e4e232c8..0007b4d4 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -437,6 +437,41 @@ class Solution: return True ``` +层序遍历 + +```python +class Solution: + def isSymmetric(self, root: TreeNode) -> bool: + if not root: return True + que, cnt = [[root.left, root.right]], 1 + while que: + nodes, tmp, sign = que.pop(), [], False + for node in nodes: + if not node: + tmp.append(None) + tmp.append(None) + else: + if node.left: + tmp.append(node.left) + sign = True + else: + tmp.append(None) + if node.right: + tmp.append(node.right) + sign = True + else: + tmp.append(None) + p1, p2 = 0, len(nodes) - 1 + while p1 < p2: + if (not nodes[p1] and nodes[p2]) or (nodes[p1] and not nodes[p2]): return False + elif nodes[p1] and nodes[p2] and nodes[p1].val != nodes[p2].val: return False + p1 += 1 + p2 -= 1 + if sign: que.append(tmp) + cnt += 1 + return True +``` + ## Go ```go