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: diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 9b59e66d..bfde6b35 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -247,7 +247,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) 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 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: 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) diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md index bf59f52d..8c13a390 100644 --- a/problems/0541.反转字符串II.md +++ b/problems/0541.反转字符串II.md @@ -227,8 +227,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) diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index 9e743887..21fc0602 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -291,8 +291,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