Skip to content

Commit e8151aa

Browse files
Merge branch 'master' into master
2 parents 3e1d341 + 345b66b commit e8151aa

8 files changed

+262
-105
lines changed

problems/0077.组合优化.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,59 @@ var combine = function(n, k) {
242242
};
243243
```
244244

245+
C:
246+
```c
247+
int* path;
248+
int pathTop;
249+
int** ans;
250+
int ansTop;
251+
252+
void backtracking(int n, int k,int startIndex) {
253+
//当path中元素个数为k个时,我们需要将path数组放入ans二维数组中
254+
if(pathTop == k) {
255+
//path数组为我们动态申请,若直接将其地址放入二维数组,path数组中的值会随着我们回溯而逐渐变化
256+
//因此创建新的数组存储path中的值
257+
int* temp = (int*)malloc(sizeof(int) * k);
258+
int i;
259+
for(i = 0; i < k; i++) {
260+
temp[i] = path[i];
261+
}
262+
ans[ansTop++] = temp;
263+
return ;
264+
}
265+
266+
int j;
267+
for(j = startIndex; j <= n- (k - pathTop) + 1;j++) {
268+
//将当前结点放入path数组
269+
path[pathTop++] = j;
270+
//进行递归
271+
backtracking(n, k, j + 1);
272+
//进行回溯,将数组最上层结点弹出
273+
pathTop--;
274+
}
275+
}
245276

277+
int** combine(int n, int k, int* returnSize, int** returnColumnSizes){
278+
//path数组存储符合条件的结果
279+
path = (int*)malloc(sizeof(int) * k);
280+
//ans二维数组存储符合条件的结果数组的集合。(数组足够大,避免极端情况)
281+
ans = (int**)malloc(sizeof(int*) * 10000);
282+
pathTop = ansTop = 0;
246283

284+
//回溯算法
285+
backtracking(n, k, 1);
286+
//最后的返回大小为ans数组大小
287+
*returnSize = ansTop;
288+
//returnColumnSizes数组存储ans二维数组对应下标中一维数组的长度(都为k)
289+
*returnColumnSizes = (int*)malloc(sizeof(int) *(*returnSize));
290+
int i;
291+
for(i = 0; i < *returnSize; i++) {
292+
(*returnColumnSizes)[i] = k;
293+
}
294+
//返回ans二维数组
295+
return ans;
296+
}
297+
```
247298
248299
-----------------------
249300
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0102.二叉树的层序遍历.md

Lines changed: 77 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,31 @@ public:
8787
8888
python代码:
8989
90-
```python
90+
```python3
9191
class Solution:
92+
"""二叉树层序遍历迭代解法"""
93+
9294
def levelOrder(self, root: TreeNode) -> List[List[int]]:
95+
results = []
9396
if not root:
94-
return []
95-
96-
queue = [root]
97-
out_list = []
98-
99-
while queue:
100-
length = len(queue)
101-
in_list = []
102-
for _ in range(length):
103-
curnode = queue.pop(0) # (默认移除列表最后一个元素)这里需要移除队列最头上的那个
104-
in_list.append(curnode.val)
105-
if curnode.left: queue.append(curnode.left)
106-
if curnode.right: queue.append(curnode.right)
107-
out_list.append(in_list)
97+
return results
98+
99+
from collections import deque
100+
que = deque([root])
101+
102+
while que:
103+
size = len(que)
104+
result = []
105+
for _ in range(size):
106+
cur = que.popleft()
107+
result.append(cur.val)
108+
if cur.left:
109+
que.append(cur.left)
110+
if cur.right:
111+
que.append(cur.right)
112+
results.append(result)
108113
109-
return out_list
114+
return results
110115
```
111116

112117
java:
@@ -274,29 +279,29 @@ python代码:
274279

275280
```python
276281
class Solution:
282+
"""二叉树层序遍历II迭代解法"""
283+
277284
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
285+
results = []
278286
if not root:
279-
return []
280-
quene = [root]
281-
out_list = []
287+
return results
282288

283-
while quene:
284-
in_list = []
285-
for _ in range(len(quene)):
286-
node = quene.pop(0)
287-
in_list.append(node.val)
288-
if node.left:
289-
quene.append(node.left)
290-
if node.right:
291-
quene.append(node.right)
292-
293-
out_list.append(in_list)
294-
295-
out_list.reverse()
296-
return out_list
297-
298-
# 执行用时:36 ms, 在所有 Python3 提交中击败了92.00%的用户
299-
# 内存消耗:15.2 MB, 在所有 Python3 提交中击败了63.76%的用户
289+
from collections import deque
290+
que = deque([root])
291+
292+
while que:
293+
result = []
294+
for _ in range(len(que)):
295+
cur = que.popleft()
296+
result.append(cur.val)
297+
if cur.left:
298+
que.append(cur.left)
299+
if cur.right:
300+
que.append(cur.right)
301+
results.append(result)
302+
303+
results.reverse()
304+
return results
300305
```
301306

302307
Java:
@@ -628,32 +633,29 @@ python代码:
628633
629634
```python
630635
class Solution:
636+
"""二叉树层平均值迭代解法"""
637+
631638
def averageOfLevels(self, root: TreeNode) -> List[float]:
639+
results = []
632640
if not root:
633-
return []
641+
return results
634642
635-
quene = deque([root])
636-
out_list = []
637-
638-
while quene:
639-
in_list = []
640-
641-
for _ in range(len(quene)):
642-
node = quene.popleft()
643-
in_list.append(node.val)
644-
if node.left:
645-
quene.append(node.left)
646-
if node.right:
647-
quene.append(node.right)
648-
649-
out_list.append(in_list)
650-
651-
out_list = map(lambda x: sum(x) / len(x), out_list)
652-
653-
return out_list
643+
from collections import deque
644+
que = deque([root])
654645
655-
# 执行用时:56 ms, 在所有 Python3 提交中击败了81.48%的用户
656-
# 内存消耗:17 MB, 在所有 Python3 提交中击败了89.68%的用户
646+
while que:
647+
size = len(que)
648+
sum_ = 0
649+
for _ in range(size):
650+
cur = que.popleft()
651+
sum_ += cur.val
652+
if cur.left:
653+
que.append(cur.left)
654+
if cur.right:
655+
que.append(cur.right)
656+
results.append(sum_ / size)
657+
658+
return results
657659
```
658660

659661
java:
@@ -823,52 +825,28 @@ public:
823825
python代码:
824826

825827
```python
826-
827828
class Solution:
829+
"""N叉树的层序遍历迭代法"""
830+
828831
def levelOrder(self, root: 'Node') -> List[List[int]]:
832+
results = []
829833
if not root:
830-
return []
834+
return results
831835

832-
quene = deque([root])
833-
out_list = []
834-
835-
while quene:
836-
in_list = []
837-
838-
for _ in range(len(quene)):
839-
node = quene.popleft()
840-
in_list.append(node.val)
841-
if node.children:
842-
# 这个地方要用extend而不是append,我们看下面的例子:
843-
# In [18]: alist=[]
844-
# In [19]: alist.append([1,2,3])
845-
# In [20]: alist
846-
# Out[20]: [[1, 2, 3]]
847-
# In [21]: alist.extend([4,5,6])
848-
# In [22]: alist
849-
# Out[22]: [[1, 2, 3], 4, 5, 6]
850-
# 可以看到extend对要添加的list进行了一个解包操作
851-
# print(root.children),可以得到children是一个包含
852-
# 孩子节点地址的list,我们使用for遍历quene的时候,
853-
# 希望quene是一个单层list,所以要用extend
854-
# 使用extend的情况,如果print(quene),结果是
855-
# deque([<__main__.Node object at 0x7f60763ae0a0>])
856-
# deque([<__main__.Node object at 0x7f607636e6d0>, <__main__.Node object at 0x7f607636e130>, <__main__.Node object at 0x7f607636e310>])
857-
# deque([<__main__.Node object at 0x7f607636e880>, <__main__.Node object at 0x7f607636ef10>])
858-
# 可以看到是单层list
859-
# 如果使用append,print(quene)的结果是
860-
# deque([<__main__.Node object at 0x7f18907530a0>])
861-
# deque([[<__main__.Node object at 0x7f18907136d0>, <__main__.Node object at 0x7f1890713130>, <__main__.Node object at 0x7f1890713310>]])
862-
# 可以看到是两层list,这样for的遍历就会报错
863-
864-
quene.extend(node.children)
865-
866-
out_list.append(in_list)
836+
from collections import deque
837+
que = deque([root])
867838

868-
return out_list
869-
870-
# 执行用时:60 ms, 在所有 Python3 提交中击败了76.99%的用户
871-
# 内存消耗:16.5 MB, 在所有 Python3 提交中击败了89.19%的用户
839+
while que:
840+
result = []
841+
for _ in range(len(que)):
842+
cur = que.popleft()
843+
result.append(cur.val)
844+
# cur.children 是 Node 对象组成的列表,也可能为 None
845+
if cur.children:
846+
que.extend(cur.children)
847+
results.append(result)
848+
849+
return results
872850
```
873851

874852
java:

problems/0383.赎金信.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ var canConstruct = function(ransomNote, magazine) {
266266
};
267267
```
268268

269+
269270
PHP:
270271
```php
271272
class Solution {
@@ -289,6 +290,28 @@ class Solution {
289290
}
290291
return true;
291292
}
293+
```
294+
295+
Swift:
296+
```swift
297+
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
298+
var record = Array(repeating: 0, count: 26);
299+
let aUnicodeScalarValue = "a".unicodeScalars.first!.value
300+
for unicodeScalar in magazine.unicodeScalars {
301+
// 通过record 记录 magazine 里各个字符出现的次数
302+
let idx: Int = Int(unicodeScalar.value - aUnicodeScalarValue)
303+
record[idx] += 1
304+
}
305+
for unicodeScalar in ransomNote.unicodeScalars {
306+
// 遍历 ransomNote,在record里对应的字符个数做 -- 操作
307+
let idx: Int = Int(unicodeScalar.value - aUnicodeScalarValue)
308+
record[idx] -= 1
309+
// 如果小于零说明在magazine没有
310+
if record[idx] < 0 {
311+
return false
312+
}
313+
}
314+
return true
292315
}
293316
```
294317

problems/0454.四数相加II.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ var fourSumCount = function(nums1, nums2, nums3, nums4) {
220220
};
221221
```
222222

223+
223224
PHP:
224225
```php
225226
class Solution {
@@ -253,6 +254,35 @@ class Solution {
253254
```
254255

255256

257+
Swift:
258+
```swift
259+
func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]) -> Int {
260+
// key:a+b的数值,value:a+b数值出现的次数
261+
var map = [Int: Int]()
262+
// 遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中
263+
for i in 0 ..< nums1.count {
264+
for j in 0 ..< nums2.count {
265+
let sum1 = nums1[i] + nums2[j]
266+
map[sum1] = (map[sum1] ?? 0) + 1
267+
}
268+
}
269+
// 统计a+b+c+d = 0 出现的次数
270+
var res = 0
271+
// 在遍历大num3和num4数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。
272+
for i in 0 ..< nums3.count {
273+
for j in 0 ..< nums4.count {
274+
let sum2 = nums3[i] + nums4[j]
275+
let other = 0 - sum2
276+
if map.keys.contains(other) {
277+
res += map[other]!
278+
}
279+
}
280+
}
281+
return res
282+
}
283+
```
284+
285+
256286
-----------------------
257287
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
258288
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

problems/0455.分发饼干.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,10 @@ func findContentChildren(g []int, s []int) int {
197197

198198
return child
199199
}
200-
200+
```
201201

202202
Javascript:
203-
```Javascript
204-
203+
```
205204
var findContentChildren = function(g, s) {
206205
g = g.sort((a, b) => a - b)
207206
s = s.sort((a, b) => a - b)

0 commit comments

Comments
 (0)