Merge branch 'youngyangyang04:master' into master

This commit is contained in:
yqq
2021-08-26 17:45:05 +08:00
committed by GitHub
3 changed files with 45 additions and 15 deletions

View File

@ -341,7 +341,7 @@ class Solution {
public boolean isValid(int row, int col, int n, char[][] chessboard) {
// 检查列
for (int i=0; i<n; ++i) {
for (int i=0; i<row; ++i) { // 相当于剪枝
if (chessboard[i][col] == 'Q') {
return false;
}

View File

@ -505,21 +505,51 @@ var maxdepth = function(root) {
二叉树最大深度层级遍历
```javascript
var maxdepth = function(root) {
//使用递归的方法 递归三部曲
//1. 确定递归函数的参数和返回值
const getdepth=function(node){
//2. 确定终止条件
if(node===null){
return 0;
var maxDepth = function(root) {
if(!root) return 0
let count = 0
const queue = [root]
while(queue.length) {
let size = queue.length
/* 层数+1 */
count++
while(size--) {
let node = queue.shift();
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
//3. 确定单层逻辑
let leftdepth=getdepth(node.left);
let rightdepth=getdepth(node.right);
let depth=1+math.max(leftdepth,rightdepth);
return depth;
}
return getDepth(root);
return count
};
```
N叉树的最大深度 递归写法
```js
var maxDepth = function(root) {
if(!root) return 0
let depth = 0
for(let node of root.children) {
depth = Math.max(depth, maxDepth(node))
}
return depth + 1
}
```
N叉树的最大深度 层序遍历
```js
var maxDepth = function(root) {
if(!root) return 0
let count = 0
let queue = [root]
while(queue.length) {
let size = queue.length
count++
while(size--) {
let node = queue.shift()
node && (queue = [...queue, ...node.children])
}
}
return count
};
```

View File

@ -181,7 +181,7 @@ class Solution:
# 统计除第一个字符串外字符的出现频率
for i in range(1, len(words)):
hashOtherStr = [0] * 26
for j in range(len(words[0])):
for j in range(len(words[i])):
hashOtherStr[ord(words[i][j]) - ord('a')] += 1
# 更新hash保证hash里统计26个字符在所有字符串里出现的最小次数
for k in range(26):