mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1117 from Camille0512/master
Added in one more python solution. Using defaultdict.
This commit is contained in:
@ -118,6 +118,18 @@ class Solution:
|
|||||||
return [records[target - val], idx] # 如果存在就返回字典记录索引和当前索引
|
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:
|
Go:
|
||||||
|
|
||||||
|
@ -247,7 +247,34 @@ class Solution:
|
|||||||
right -= 1
|
right -= 1
|
||||||
return ans
|
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:
|
||||||
|
|
||||||
```Go
|
```Go
|
||||||
func threeSum(nums []int)[][]int{
|
func threeSum(nums []int)[][]int{
|
||||||
sort.Ints(nums)
|
sort.Ints(nums)
|
||||||
|
@ -437,6 +437,41 @@ class Solution:
|
|||||||
return True
|
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
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -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:
|
Go:
|
||||||
|
|
||||||
|
@ -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:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
|
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) int {
|
||||||
m := make(map[int]int)
|
m := make(map[int]int)
|
||||||
|
@ -227,8 +227,23 @@ class Solution:
|
|||||||
return ''.join(res)
|
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:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func reverseStr(s string, k int) string {
|
func reverseStr(s string, k int) string {
|
||||||
ss := []byte(s)
|
ss := []byte(s)
|
||||||
|
@ -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:
|
javaScript:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
/**
|
/**
|
||||||
* @param {string} s
|
* @param {string} s
|
||||||
|
Reference in New Issue
Block a user