Skip to content

Commit a889eb0

Browse files
authored
Merge pull request azl397985856#520 from andy814/master
2 parents 6173063 + 2a46199 commit a889eb0

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

problems/437.path-sum-iii.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ var pathSum = function (root, sum) {
117117

118118
## 代码
119119

120-
- 语言支持:JS
120+
- 语言支持:JS, Python
121121

122+
JS code:
122123
```js
123124
/*
124125
* @lc app=leetcode id=437 lang=javascript
@@ -164,6 +165,39 @@ var pathSum = function (root, sum) {
164165
};
165166
```
166167

168+
Python Code:
169+
```python
170+
import collections
171+
'''
172+
class TreeNode:
173+
def __init__(self, val=0, left=None, right=None):
174+
self.val = val
175+
self.left = left
176+
self.right = right
177+
'''
178+
class Solution:
179+
def helper(self,root,acc,target,hashmap):
180+
if not root:
181+
return 0
182+
count=0
183+
acc+=root.val
184+
if acc==target:
185+
count+=1
186+
if acc-target in hashmap:
187+
count+=hashmap[acc-target]
188+
hashmap[acc]+=1
189+
if root.left:
190+
count+=self.helper(root.left,acc,target,hashmap)
191+
if root.right:
192+
count+=self.helper(root.right,acc,target,hashmap)
193+
hashmap[acc]-=1
194+
return count
195+
196+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
197+
hashmap=collections.defaultdict(lambda:0)
198+
return self.helper(root,0,targetSum,hashmap)
199+
```
200+
167201
**复杂度分析**
168202

169203
- 时间复杂度:$O(N)$

problems/455.AssignCookies.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ https://leetcode-cn.com/problems/assign-cookies/
6161

6262
## 代码
6363

64-
语言支持:JS
64+
语言支持:JS, Python
6565

66+
JS Code:
6667
```js
6768
/**
6869
* @param {number[]} g
@@ -90,6 +91,21 @@ const findContentChildren = function (g, s) {
9091
};
9192
```
9293

94+
Python Code:
95+
```python
96+
class Solution:
97+
def findContentChildren(self, g: List[int], s: List[int]) -> int:
98+
g.sort()
99+
s.sort()
100+
count=gIdx=sIdx=0
101+
while gIdx<len(g) and sIdx<len(s):
102+
if s[sIdx]>=g[gIdx]:
103+
gIdx+=1
104+
count+=1
105+
sIdx+=1
106+
return count
107+
```
108+
93109
***复杂度分析***
94110

95111
- 时间复杂度:由于使用了排序,因此时间复杂度为 O(NlogN)

0 commit comments

Comments
 (0)