Skip to content

Commit a81b502

Browse files
committed
feat: add solutions to lc problem: No.0666
No.0666.Path Sum IV
1 parent a4204c4 commit a81b502

File tree

6 files changed

+334
-2
lines changed

6 files changed

+334
-2
lines changed

solution/0600-0699/0666.Path Sum IV/README.md

+115-1
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,136 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
DFS。
58+
5759
<!-- tabs:start -->
5860

5961
### **Python3**
6062

6163
<!-- 这里可写当前语言的特殊实现逻辑 -->
6264

6365
```python
64-
66+
class Solution:
67+
def pathSum(self, nums: List[int]) -> int:
68+
def dfs(node, t):
69+
if node not in mp:
70+
return
71+
t += mp[node]
72+
d, p = divmod(node, 10)
73+
l = (d + 1) * 10 + (p * 2) - 1
74+
r = l + 1
75+
nonlocal ans
76+
if l not in mp and r not in mp:
77+
ans += t
78+
return
79+
dfs(l, t)
80+
dfs(r, t)
81+
82+
ans = 0
83+
mp = {num // 10: num % 10 for num in nums}
84+
dfs(11, 0)
85+
return ans
6586
```
6687

6788
### **Java**
6889

6990
<!-- 这里可写当前语言的特殊实现逻辑 -->
7091

7192
```java
93+
class Solution {
94+
private int ans;
95+
private Map<Integer, Integer> mp;
96+
97+
public int pathSum(int[] nums) {
98+
ans = 0;
99+
mp = new HashMap<>(nums.length);
100+
for (int num : nums) {
101+
mp.put(num / 10, num % 10);
102+
}
103+
dfs(11, 0);
104+
return ans;
105+
}
106+
107+
private void dfs(int node, int t) {
108+
if (!mp.containsKey(node)) {
109+
return;
110+
}
111+
t += mp.get(node);
112+
int d = node / 10, p = node % 10;
113+
int l = (d + 1) * 10 + (p * 2) - 1;
114+
int r = l + 1;
115+
if (!mp.containsKey(l) && !mp.containsKey(r)) {
116+
ans += t;
117+
return;
118+
}
119+
dfs(l, t);
120+
dfs(r, t);
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
int ans;
131+
unordered_map<int, int> mp;
132+
133+
int pathSum(vector<int>& nums) {
134+
ans = 0;
135+
mp.clear();
136+
for (int num : nums) mp[num / 10] = num % 10;
137+
dfs(11, 0);
138+
return ans;
139+
}
140+
141+
void dfs(int node, int t) {
142+
if (!mp.count(node)) return;
143+
t += mp[node];
144+
int d = node / 10, p = node % 10;
145+
int l = (d + 1) * 10 + (p * 2) - 1;
146+
int r = l + 1;
147+
if (!mp.count(l) && !mp.count(r))
148+
{
149+
ans += t;
150+
return;
151+
}
152+
dfs(l, t);
153+
dfs(r, t);
154+
}
155+
};
156+
```
72157
158+
### **Go**
159+
160+
```go
161+
func pathSum(nums []int) int {
162+
ans := 0
163+
mp := make(map[int]int)
164+
for _, num := range nums {
165+
mp[num/10] = num % 10
166+
}
167+
var dfs func(node, t int)
168+
dfs = func(node, t int) {
169+
if v, ok := mp[node]; ok {
170+
t += v
171+
d, p := node/10, node%10
172+
l := (d+1)*10 + (p * 2) - 1
173+
r := l + 1
174+
if _, ok1 := mp[l]; !ok1 {
175+
if _, ok2 := mp[r]; !ok2 {
176+
ans += t
177+
return
178+
}
179+
}
180+
dfs(l, t)
181+
dfs(r, t)
182+
}
183+
}
184+
dfs(11, 0)
185+
return ans
186+
}
73187
```
74188

75189
### **...**

solution/0600-0699/0666.Path Sum IV/README_EN.md

+115-1
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,132 @@ The path sum is (3 + 1) = 4.
5050

5151
## Solutions
5252

53+
DFS.
54+
5355
<!-- tabs:start -->
5456

5557
### **Python3**
5658

5759
```python
58-
60+
class Solution:
61+
def pathSum(self, nums: List[int]) -> int:
62+
def dfs(node, t):
63+
if node not in mp:
64+
return
65+
t += mp[node]
66+
d, p = divmod(node, 10)
67+
l = (d + 1) * 10 + (p * 2) - 1
68+
r = l + 1
69+
nonlocal ans
70+
if l not in mp and r not in mp:
71+
ans += t
72+
return
73+
dfs(l, t)
74+
dfs(r, t)
75+
76+
ans = 0
77+
mp = {num // 10: num % 10 for num in nums}
78+
dfs(11, 0)
79+
return ans
5980
```
6081

6182
### **Java**
6283

6384
```java
85+
class Solution {
86+
private int ans;
87+
private Map<Integer, Integer> mp;
88+
89+
public int pathSum(int[] nums) {
90+
ans = 0;
91+
mp = new HashMap<>(nums.length);
92+
for (int num : nums) {
93+
mp.put(num / 10, num % 10);
94+
}
95+
dfs(11, 0);
96+
return ans;
97+
}
98+
99+
private void dfs(int node, int t) {
100+
if (!mp.containsKey(node)) {
101+
return;
102+
}
103+
t += mp.get(node);
104+
int d = node / 10, p = node % 10;
105+
int l = (d + 1) * 10 + (p * 2) - 1;
106+
int r = l + 1;
107+
if (!mp.containsKey(l) && !mp.containsKey(r)) {
108+
ans += t;
109+
return;
110+
}
111+
dfs(l, t);
112+
dfs(r, t);
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int ans;
123+
unordered_map<int, int> mp;
124+
125+
int pathSum(vector<int>& nums) {
126+
ans = 0;
127+
mp.clear();
128+
for (int num : nums) mp[num / 10] = num % 10;
129+
dfs(11, 0);
130+
return ans;
131+
}
132+
133+
void dfs(int node, int t) {
134+
if (!mp.count(node)) return;
135+
t += mp[node];
136+
int d = node / 10, p = node % 10;
137+
int l = (d + 1) * 10 + (p * 2) - 1;
138+
int r = l + 1;
139+
if (!mp.count(l) && !mp.count(r))
140+
{
141+
ans += t;
142+
return;
143+
}
144+
dfs(l, t);
145+
dfs(r, t);
146+
}
147+
};
148+
```
64149
150+
### **Go**
151+
152+
```go
153+
func pathSum(nums []int) int {
154+
ans := 0
155+
mp := make(map[int]int)
156+
for _, num := range nums {
157+
mp[num/10] = num % 10
158+
}
159+
var dfs func(node, t int)
160+
dfs = func(node, t int) {
161+
if v, ok := mp[node]; ok {
162+
t += v
163+
d, p := node/10, node%10
164+
l := (d+1)*10 + (p * 2) - 1
165+
r := l + 1
166+
if _, ok1 := mp[l]; !ok1 {
167+
if _, ok2 := mp[r]; !ok2 {
168+
ans += t
169+
return
170+
}
171+
}
172+
dfs(l, t)
173+
dfs(r, t)
174+
}
175+
}
176+
dfs(11, 0)
177+
return ans
178+
}
65179
```
66180

67181
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int ans;
4+
unordered_map<int, int> mp;
5+
6+
int pathSum(vector<int>& nums) {
7+
ans = 0;
8+
mp.clear();
9+
for (int num : nums) mp[num / 10] = num % 10;
10+
dfs(11, 0);
11+
return ans;
12+
}
13+
14+
void dfs(int node, int t) {
15+
if (!mp.count(node)) return;
16+
t += mp[node];
17+
int d = node / 10, p = node % 10;
18+
int l = (d + 1) * 10 + (p * 2) - 1;
19+
int r = l + 1;
20+
if (!mp.count(l) && !mp.count(r))
21+
{
22+
ans += t;
23+
return;
24+
}
25+
dfs(l, t);
26+
dfs(r, t);
27+
}
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func pathSum(nums []int) int {
2+
ans := 0
3+
mp := make(map[int]int)
4+
for _, num := range nums {
5+
mp[num/10] = num % 10
6+
}
7+
var dfs func(node, t int)
8+
dfs = func(node, t int) {
9+
if v, ok := mp[node]; ok {
10+
t += v
11+
d, p := node/10, node%10
12+
l := (d+1)*10 + (p * 2) - 1
13+
r := l + 1
14+
if _, ok1 := mp[l]; !ok1 {
15+
if _, ok2 := mp[r]; !ok2 {
16+
ans += t
17+
return
18+
}
19+
}
20+
dfs(l, t)
21+
dfs(r, t)
22+
}
23+
}
24+
dfs(11, 0)
25+
return ans
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
private int ans;
3+
private Map<Integer, Integer> mp;
4+
5+
public int pathSum(int[] nums) {
6+
ans = 0;
7+
mp = new HashMap<>(nums.length);
8+
for (int num : nums) {
9+
mp.put(num / 10, num % 10);
10+
}
11+
dfs(11, 0);
12+
return ans;
13+
}
14+
15+
private void dfs(int node, int t) {
16+
if (!mp.containsKey(node)) {
17+
return;
18+
}
19+
t += mp.get(node);
20+
int d = node / 10, p = node % 10;
21+
int l = (d + 1) * 10 + (p * 2) - 1;
22+
int r = l + 1;
23+
if (!mp.containsKey(l) && !mp.containsKey(r)) {
24+
ans += t;
25+
return;
26+
}
27+
dfs(l, t);
28+
dfs(r, t);
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def pathSum(self, nums: List[int]) -> int:
3+
def dfs(node, t):
4+
if node not in mp:
5+
return
6+
t += mp[node]
7+
d, p = divmod(node, 10)
8+
l = (d + 1) * 10 + (p * 2) - 1
9+
r = l + 1
10+
nonlocal ans
11+
if l not in mp and r not in mp:
12+
ans += t
13+
return
14+
dfs(l, t)
15+
dfs(r, t)
16+
17+
ans = 0
18+
mp = {num // 10: num % 10 for num in nums}
19+
dfs(11, 0)
20+
return ans

0 commit comments

Comments
 (0)