Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 authored Sep 4, 2021
2 parents 3e1d341 + 345b66b commit e8151aa
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 105 deletions.
51 changes: 51 additions & 0 deletions problems/0077.组合优化.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,59 @@ var combine = function(n, k) {
};
```

C:
```c
int* path;
int pathTop;
int** ans;
int ansTop;

void backtracking(int n, int k,int startIndex) {
//当path中元素个数为k个时,我们需要将path数组放入ans二维数组中
if(pathTop == k) {
//path数组为我们动态申请,若直接将其地址放入二维数组,path数组中的值会随着我们回溯而逐渐变化
//因此创建新的数组存储path中的值
int* temp = (int*)malloc(sizeof(int) * k);
int i;
for(i = 0; i < k; i++) {
temp[i] = path[i];
}
ans[ansTop++] = temp;
return ;
}

int j;
for(j = startIndex; j <= n- (k - pathTop) + 1;j++) {
//将当前结点放入path数组
path[pathTop++] = j;
//进行递归
backtracking(n, k, j + 1);
//进行回溯,将数组最上层结点弹出
pathTop--;
}
}

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

//回溯算法
backtracking(n, k, 1);
//最后的返回大小为ans数组大小
*returnSize = ansTop;
//returnColumnSizes数组存储ans二维数组对应下标中一维数组的长度(都为k)
*returnColumnSizes = (int*)malloc(sizeof(int) *(*returnSize));
int i;
for(i = 0; i < *returnSize; i++) {
(*returnColumnSizes)[i] = k;
}
//返回ans二维数组
return ans;
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
Expand Down
176 changes: 77 additions & 99 deletions problems/0102.二叉树的层序遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,31 @@ public:
python代码:
```python
```python3
class Solution:
"""二叉树层序遍历迭代解法"""
def levelOrder(self, root: TreeNode) -> List[List[int]]:
results = []
if not root:
return []
queue = [root]
out_list = []
while queue:
length = len(queue)
in_list = []
for _ in range(length):
curnode = queue.pop(0) # (默认移除列表最后一个元素)这里需要移除队列最头上的那个
in_list.append(curnode.val)
if curnode.left: queue.append(curnode.left)
if curnode.right: queue.append(curnode.right)
out_list.append(in_list)
return results
from collections import deque
que = deque([root])
while que:
size = len(que)
result = []
for _ in range(size):
cur = que.popleft()
result.append(cur.val)
if cur.left:
que.append(cur.left)
if cur.right:
que.append(cur.right)
results.append(result)
return out_list
return results
```

java:
Expand Down Expand Up @@ -274,29 +279,29 @@ python代码:

```python
class Solution:
"""二叉树层序遍历II迭代解法"""

def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
results = []
if not root:
return []
quene = [root]
out_list = []
return results

while quene:
in_list = []
for _ in range(len(quene)):
node = quene.pop(0)
in_list.append(node.val)
if node.left:
quene.append(node.left)
if node.right:
quene.append(node.right)

out_list.append(in_list)

out_list.reverse()
return out_list

# 执行用时:36 ms, 在所有 Python3 提交中击败了92.00%的用户
# 内存消耗:15.2 MB, 在所有 Python3 提交中击败了63.76%的用户
from collections import deque
que = deque([root])

while que:
result = []
for _ in range(len(que)):
cur = que.popleft()
result.append(cur.val)
if cur.left:
que.append(cur.left)
if cur.right:
que.append(cur.right)
results.append(result)

results.reverse()
return results
```

Java:
Expand Down Expand Up @@ -628,32 +633,29 @@ python代码:
```python
class Solution:
"""二叉树层平均值迭代解法"""
def averageOfLevels(self, root: TreeNode) -> List[float]:
results = []
if not root:
return []
return results
quene = deque([root])
out_list = []
while quene:
in_list = []
for _ in range(len(quene)):
node = quene.popleft()
in_list.append(node.val)
if node.left:
quene.append(node.left)
if node.right:
quene.append(node.right)
out_list.append(in_list)
out_list = map(lambda x: sum(x) / len(x), out_list)
return out_list
from collections import deque
que = deque([root])
# 执行用时:56 ms, 在所有 Python3 提交中击败了81.48%的用户
# 内存消耗:17 MB, 在所有 Python3 提交中击败了89.68%的用户
while que:
size = len(que)
sum_ = 0
for _ in range(size):
cur = que.popleft()
sum_ += cur.val
if cur.left:
que.append(cur.left)
if cur.right:
que.append(cur.right)
results.append(sum_ / size)
return results
```

java:
Expand Down Expand Up @@ -823,52 +825,28 @@ public:
python代码:

```python

class Solution:
"""N叉树的层序遍历迭代法"""

def levelOrder(self, root: 'Node') -> List[List[int]]:
results = []
if not root:
return []
return results

quene = deque([root])
out_list = []

while quene:
in_list = []

for _ in range(len(quene)):
node = quene.popleft()
in_list.append(node.val)
if node.children:
# 这个地方要用extend而不是append,我们看下面的例子:
# In [18]: alist=[]
# In [19]: alist.append([1,2,3])
# In [20]: alist
# Out[20]: [[1, 2, 3]]
# In [21]: alist.extend([4,5,6])
# In [22]: alist
# Out[22]: [[1, 2, 3], 4, 5, 6]
# 可以看到extend对要添加的list进行了一个解包操作
# print(root.children),可以得到children是一个包含
# 孩子节点地址的list,我们使用for遍历quene的时候,
# 希望quene是一个单层list,所以要用extend
# 使用extend的情况,如果print(quene),结果是
# deque([<__main__.Node object at 0x7f60763ae0a0>])
# deque([<__main__.Node object at 0x7f607636e6d0>, <__main__.Node object at 0x7f607636e130>, <__main__.Node object at 0x7f607636e310>])
# deque([<__main__.Node object at 0x7f607636e880>, <__main__.Node object at 0x7f607636ef10>])
# 可以看到是单层list
# 如果使用append,print(quene)的结果是
# deque([<__main__.Node object at 0x7f18907530a0>])
# deque([[<__main__.Node object at 0x7f18907136d0>, <__main__.Node object at 0x7f1890713130>, <__main__.Node object at 0x7f1890713310>]])
# 可以看到是两层list,这样for的遍历就会报错

quene.extend(node.children)

out_list.append(in_list)
from collections import deque
que = deque([root])

return out_list

# 执行用时:60 ms, 在所有 Python3 提交中击败了76.99%的用户
# 内存消耗:16.5 MB, 在所有 Python3 提交中击败了89.19%的用户
while que:
result = []
for _ in range(len(que)):
cur = que.popleft()
result.append(cur.val)
# cur.children 是 Node 对象组成的列表,也可能为 None
if cur.children:
que.extend(cur.children)
results.append(result)

return results
```

java:
Expand Down
23 changes: 23 additions & 0 deletions problems/0383.赎金信.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ var canConstruct = function(ransomNote, magazine) {
};
```


PHP:
```php
class Solution {
Expand All @@ -289,6 +290,28 @@ class Solution {
}
return true;
}
```

Swift:
```swift
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
var record = Array(repeating: 0, count: 26);
let aUnicodeScalarValue = "a".unicodeScalars.first!.value
for unicodeScalar in magazine.unicodeScalars {
// 通过record 记录 magazine 里各个字符出现的次数
let idx: Int = Int(unicodeScalar.value - aUnicodeScalarValue)
record[idx] += 1
}
for unicodeScalar in ransomNote.unicodeScalars {
// 遍历 ransomNote,在record里对应的字符个数做 -- 操作
let idx: Int = Int(unicodeScalar.value - aUnicodeScalarValue)
record[idx] -= 1
// 如果小于零说明在magazine没有
if record[idx] < 0 {
return false
}
}
return true
}
```

Expand Down
30 changes: 30 additions & 0 deletions problems/0454.四数相加II.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ var fourSumCount = function(nums1, nums2, nums3, nums4) {
};
```


PHP:
```php
class Solution {
Expand Down Expand Up @@ -253,6 +254,35 @@ class Solution {
```


Swift:
```swift
func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]) -> Int {
// key:a+b的数值,value:a+b数值出现的次数
var map = [Int: Int]()
// 遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中
for i in 0 ..< nums1.count {
for j in 0 ..< nums2.count {
let sum1 = nums1[i] + nums2[j]
map[sum1] = (map[sum1] ?? 0) + 1
}
}
// 统计a+b+c+d = 0 出现的次数
var res = 0
// 在遍历大num3和num4数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。
for i in 0 ..< nums3.count {
for j in 0 ..< nums4.count {
let sum2 = nums3[i] + nums4[j]
let other = 0 - sum2
if map.keys.contains(other) {
res += map[other]!
}
}
}
return res
}
```


-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
Expand Down
5 changes: 2 additions & 3 deletions problems/0455.分发饼干.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,10 @@ func findContentChildren(g []int, s []int) int {

return child
}

```

Javascript:
```Javascript
```
var findContentChildren = function(g, s) {
g = g.sort((a, b) => a - b)
s = s.sort((a, b) => a - b)
Expand Down
Loading

0 comments on commit e8151aa

Please sign in to comment.