Skip to content

Commit

Permalink
Merge pull request #2524 from awsome-knowledge/master
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
youngyangyang04 committed May 10, 2024
2 parents e718d9b + 5e6a657 commit c5250fa
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 182 deletions.
24 changes: 14 additions & 10 deletions problems/0019.删除链表的倒数第N个节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]

示例 2:

输入:head = [1], n = 1
输出:[]

示例 3:

输入:head = [1,2], n = 1
Expand Down Expand Up @@ -192,16 +194,18 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {
let ret = new ListNode(0, head),
slow = fast = ret;
while(n--) fast = fast.next;
while (fast.next !== null) {
fast = fast.next;
slow = slow.next
};
slow.next = slow.next.next;
return ret.next;
var removeNthFromEnd = function (head, n) {
// 创建哨兵节点,简化解题逻辑
let dummyHead = new ListNode(0, head);
let fast = dummyHead;
let slow = dummyHead;
while (n--) fast = fast.next;
while (fast.next !== null) {
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummyHead.next;
};
```
### TypeScript:
Expand Down
64 changes: 47 additions & 17 deletions problems/0077.组合.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,28 +469,58 @@ func dfs(n int, k int, start int) {
```

### Javascript
未剪枝:

```js
var combine = function (n, k) {
// 回溯法
let result = [],
path = [];
let backtracking = (_n, _k, startIndex) => {
// 终止条件
if (path.length === _k) {
result.push(path.slice());
return;
}
// 循环本层集合元素
for (let i = startIndex; i <= _n; i++) {
path.push(i);
// 递归
backtracking(_n, _k, i + 1);
// 回溯操作
path.pop();
}
};
backtracking(n, k, 1);
return result;
};
```

剪枝:

```javascript
let result = []
let path = []
var combine = function(n, k) {
result = []
combineHelper(n, k, 1)
return result
var combine = function (n, k) {
// 回溯法
let result = [],
path = [];
let backtracking = (_n, _k, startIndex) => {
// 终止条件
if (path.length === _k) {
result.push(path.slice());
return;
}
// 循环本层集合元素
for (let i = startIndex; i <= _n - (_k - path.length) + 1; i++) {
path.push(i);
// 递归
backtracking(_n, _k, i + 1);
// 回溯操作
path.pop();
}
};
backtracking(n, k, 1);
return result;
};
const combineHelper = (n, k, startIndex) => {
if (path.length === k) {
result.push([...path])
return
}
for (let i = startIndex; i <= n - (k - path.length) + 1; ++i) {
path.push(i)
combineHelper(n, k, i + 1)
path.pop()
}
}
```

### TypeScript
Expand Down
154 changes: 80 additions & 74 deletions problems/0102.二叉树的层序遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -692,27 +692,29 @@ func levelOrderBottom(root *TreeNode) [][]int {
#### Javascript:

```javascript
var levelOrderBottom = function(root) {
let res = [], queue = [];
queue.push(root);
while(queue.length && root!==null) {
// 存放当前层级节点数组
let curLevel = [];
// 计算当前层级节点数量
let length = queue.length;
while(length--) {
let node = queue.shift();
// 把当前层节点存入curLevel数组
curLevel.push(node.val);
// 把下一层级的左右节点存入queue队列
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
// 从数组前头插入值,避免最后反转数组,减少运算时间
res.unshift(curLevel);
}
return res;
var levelOrderBottom = function (root) {
let res = [],
queue = [];
queue.push(root);
while (queue.length && root !== null) {
// 存放当前层级节点数组
let curLevel = [];
// 计算当前层级节点数量
let length = queue.length;
while (length--) {
let node = queue.shift();
// 把当前层节点存入curLevel数组
curLevel.push(node.val);
// 把下一层级的左右节点存入queue队列
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
// 从数组前头插入值,避免最后反转数组,减少运算时间
res.unshift(curLevel);
}
return res;
};

```

#### TypeScript:
Expand Down Expand Up @@ -1140,7 +1142,7 @@ impl Solution {

### 思路

本题就是层序遍历的时候把一层求个总和在取一个均值
本题就是层序遍历的时候把一层求个总和再取一个均值

C++代码:

Expand Down Expand Up @@ -1295,26 +1297,26 @@ func averageOfLevels(root *TreeNode) []float64 {

```javascript
var averageOfLevels = function(root) {
//层级平均值
let res = [], queue = [];
queue.push(root);

while(queue.length && root!==null) {
//每一层节点个数
let length = queue.length;
//sum记录每一层的和
let sum = 0;
for(let i=0; i < length; i++) {
let node = queue.shift();
sum += node.val;
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
//每一层的平均值存入数组res
res.push(sum/length);
}

return res;
let res = [],
queue = [];
queue.push(root);
while (queue.length) {
// 每一层节点个数;
let lengthLevel = queue.length,
len = queue.length,
// sum记录每一层的和;
sum = 0;
while (lengthLevel--) {
const node = queue.shift();
sum += node.val;
// 队列存放下一层节点
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
// 求平均值
res.push(sum / len);
}
return res;
};
```

Expand Down Expand Up @@ -1925,26 +1927,28 @@ func max(x, y int) int {
#### Javascript

```javascript
var largestValues = function(root) {
//使用层序遍历
let res = [], queue = [];
queue.push(root);

while(root !== null && queue.length) {
//设置max初始值就是队列的第一个元素
let max = queue[0].val;
let length = queue.length;
while(length--) {
let node = queue.shift();
max = max > node.val ? max : node.val;
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
//把每一层的最大值放到res数组
res.push(max);
}

var largestValues = function (root) {
let res = [],
queue = [];
queue.push(root);
if (root === null) {
return res;
}
while (queue.length) {
let lengthLevel = queue.length,
// 初始值设为负无穷大
max = -Infinity;
while (lengthLevel--) {
const node = queue.shift();
// 在当前层中找到最大值
max = Math.max(max, node.val);
// 找到下一层的节点
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
res.push(max);
}
return res;
};
```

Expand Down Expand Up @@ -2805,21 +2809,23 @@ func maxDepth(root *TreeNode) int {
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
// 最大的深度就是二叉树的层数
if (root === null) return 0;
let queue = [root];
let height = 0;
while (queue.length) {
let n = queue.length;
height++;
for (let i=0; i<n; i++) {
let node = queue.shift();
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
var maxDepth = function (root) {
// 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
let max = 0,
queue = [root];
if (root === null) {
return max;
}
while (queue.length) {
max++;
let length = queue.length;
while (length--) {
let node = queue.shift();
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
return height;
}
return max;
};
```

Expand Down
29 changes: 14 additions & 15 deletions problems/0209.长度最小的子数组.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,21 @@ var minSubArrayLen = function(target, nums) {

```typescript
function minSubArrayLen(target: number, nums: number[]): number {
let left: number = 0, right: number = 0;
let res: number = nums.length + 1;
let sum: number = 0;
while (right < nums.length) {
sum += nums[right];
if (sum >= target) {
// 不断移动左指针,直到不能再缩小为止
while (sum - nums[left] >= target) {
sum -= nums[left++];
}
res = Math.min(res, right - left + 1);
}
right++;
let left: number = 0,
res: number = Infinity,
subLen: number = 0,
sum: number = 0;
for (let right: number = 0; right < nums.length; right++) {
sum += nums[right];
while (sum >= target) {
subLen = right - left + 1;
res = Math.min(res, subLen);
sum -= nums[left];
left++;
}
return res === nums.length + 1 ? 0 : res;
};
}
return res === Infinity ? 0 : res;
}
```

### Swift:
Expand Down

0 comments on commit c5250fa

Please sign in to comment.