Merge branch 'youngyangyang04:master' into master

This commit is contained in:
Luo
2022-02-21 17:07:34 +08:00
committed by GitHub
39 changed files with 961 additions and 148 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -142,6 +142,7 @@ public:
### Java
```Java
// 版本一
class Solution {
public int jump(int[] nums) {
if (nums == null || nums.length == 0 || nums.length == 1) {
@ -172,7 +173,30 @@ class Solution {
}
```
```java
// 版本二
class Solution {
public int jump(int[] nums) {
int result = 0;
// 当前覆盖的最远距离下标
int end = 0;
// 下一步覆盖的最远距离下标
int temp = 0;
for (int i = 0; i <= end && end < nums.length - 1; ++i) {
temp = Math.max(temp, i + nums[i]);
// 可达位置的改变次数就是跳跃次数
if (i == end) {
end = temp;
result++;
}
}
return result;
}
}
```
### Python
```python
class Solution:
def jump(self, nums: List[int]) -> int:

View File

@ -574,6 +574,75 @@ var isSymmetric = function(root) {
};
```
## TypeScript
> 递归法
```typescript
function isSymmetric(root: TreeNode | null): boolean {
function recur(node1: TreeNode | null, node2: TreeNode | null): boolean {
if (node1 === null && node2 === null) return true;
if (node1 === null || node2 === null) return false;
if (node1.val !== node2.val) return false
let isSym1: boolean = recur(node1.left, node2.right);
let isSym2: boolean = recur(node1.right, node2.left);
return isSym1 && isSym2
}
if (root === null) return true;
return recur(root.left, root.right);
};
```
> 迭代法
```typescript
// 迭代法(队列)
function isSymmetric(root: TreeNode | null): boolean {
let helperQueue: (TreeNode | null)[] = [];
let tempNode1: TreeNode | null,
tempNode2: TreeNode | null;
if (root !== null) {
helperQueue.push(root.left);
helperQueue.push(root.right);
}
while (helperQueue.length > 0) {
tempNode1 = helperQueue.shift()!;
tempNode2 = helperQueue.shift()!;
if (tempNode1 === null && tempNode2 === null) continue;
if (tempNode1 === null || tempNode2 === null) return false;
if (tempNode1.val !== tempNode2.val) return false;
helperQueue.push(tempNode1.left);
helperQueue.push(tempNode2.right);
helperQueue.push(tempNode1.right);
helperQueue.push(tempNode2.left);
}
return true;
}
// 迭代法(栈)
function isSymmetric(root: TreeNode | null): boolean {
let helperStack: (TreeNode | null)[] = [];
let tempNode1: TreeNode | null,
tempNode2: TreeNode | null;
if (root !== null) {
helperStack.push(root.left);
helperStack.push(root.right);
}
while (helperStack.length > 0) {
tempNode1 = helperStack.pop()!;
tempNode2 = helperStack.pop()!;
if (tempNode1 === null && tempNode2 === null) continue;
if (tempNode1 === null || tempNode2 === null) return false;
if (tempNode1.val !== tempNode2.val) return false;
helperStack.push(tempNode1.left);
helperStack.push(tempNode2.right);
helperStack.push(tempNode1.right);
helperStack.push(tempNode2.left);
}
return true;
};
```
## Swift:
> 递归

View File

@ -246,7 +246,35 @@ var levelOrder = function(root) {
```
TypeScript
```typescript
function levelOrder(root: TreeNode | null): number[][] {
let helperQueue: TreeNode[] = [];
let res: number[][] = [];
let tempArr: number[] = [];
if (root !== null) helperQueue.push(root);
let curNode: TreeNode;
while (helperQueue.length > 0) {
for (let i = 0, length = helperQueue.length; i < length; i++) {
curNode = helperQueue.shift()!;
tempArr.push(curNode.val);
if (curNode.left !== null) {
helperQueue.push(curNode.left);
}
if (curNode.right !== null) {
helperQueue.push(curNode.right);
}
}
res.push(tempArr);
tempArr = [];
}
return res;
};
```
Swift:
```swift
func levelOrder(_ root: TreeNode?) -> [[Int]] {
var res = [[Int]]()
@ -454,7 +482,31 @@ var levelOrderBottom = function(root) {
};
```
TypeScript:
```typescript
function levelOrderBottom(root: TreeNode | null): number[][] {
let helperQueue: TreeNode[] = [];
let resArr: number[][] = [];
let tempArr: number[] = [];
let tempNode: TreeNode;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
for (let i = 0, length = helperQueue.length; i < length; i++) {
tempNode = helperQueue.shift()!;
tempArr.push(tempNode.val);
if (tempNode.left !== null) helperQueue.push(tempNode.left);
if (tempNode.right !== null) helperQueue.push(tempNode.right);
}
resArr.push(tempArr);
tempArr = [];
}
return resArr.reverse();
};
```
Swift:
```swift
func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
var res = [[Int]]()
@ -657,7 +709,28 @@ var rightSideView = function(root) {
};
```
TypeScript
```typescript
function rightSideView(root: TreeNode | null): number[] {
let helperQueue: TreeNode[] = [];
let resArr: number[] = [];
let tempNode: TreeNode;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
for (let i = 0, length = helperQueue.length; i < length; i++) {
tempNode = helperQueue.shift()!;
if (i === length - 1) resArr.push(tempNode.val);
if (tempNode.left !== null) helperQueue.push(tempNode.left);
if (tempNode.right !== null) helperQueue.push(tempNode.right);
}
}
return resArr;
};
```
Swift:
```swift
func rightSideView(_ root: TreeNode?) -> [Int] {
var res = [Int]()
@ -868,7 +941,32 @@ var averageOfLevels = function(root) {
};
```
TypeScript
```typescript
function averageOfLevels(root: TreeNode | null): number[] {
let helperQueue: TreeNode[] = [];
let resArr: number[] = [];
let total: number = 0;
let tempNode: TreeNode;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
let length = helperQueue.length;
for (let i = 0; i < length; i++) {
tempNode = helperQueue.shift()!;
total += tempNode.val;
if (tempNode.left) helperQueue.push(tempNode.left);
if (tempNode.right) helperQueue.push(tempNode.right);
}
resArr.push(total / length);
total = 0;
}
return resArr;
};
```
Swift:
```swift
func averageOfLevels(_ root: TreeNode?) -> [Double] {
var res = [Double]()
@ -1092,7 +1190,30 @@ var levelOrder = function(root) {
};
```
TypeScript:
```typescript
function levelOrder(root: Node | null): number[][] {
let helperQueue: Node[] = [];
let resArr: number[][] = [];
let tempArr: number[] = [];
if (root !== null) helperQueue.push(root);
let curNode: Node;
while (helperQueue.length > 0) {
for (let i = 0, length = helperQueue.length; i < length; i++) {
curNode = helperQueue.shift()!;
tempArr.push(curNode.val);
helperQueue.push(...curNode.children);
}
resArr.push(tempArr);
tempArr = [];
}
return resArr;
};
```
Swift:
```swift
func levelOrder(_ root: Node?) -> [[Int]] {
var res = [[Int]]()
@ -1272,7 +1393,34 @@ var largestValues = function(root) {
};
```
TypeScript:
```typescript
function largestValues(root: TreeNode | null): number[] {
let helperQueue: TreeNode[] = [];
let resArr: number[] = [];
let tempNode: TreeNode;
let max: number = 0;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
for (let i = 0, length = helperQueue.length; i < length; i++) {
tempNode = helperQueue.shift()!;
if (i === 0) {
max = tempNode.val;
} else {
max = max > tempNode.val ? max : tempNode.val;
}
if (tempNode.left) helperQueue.push(tempNode.left);
if (tempNode.right) helperQueue.push(tempNode.right);
}
resArr.push(max);
}
return resArr;
};
```
Swift:
```swift
func largestValues(_ root: TreeNode?) -> [Int] {
var res = [Int]()
@ -1463,6 +1611,31 @@ var connect = function(root) {
};
```
TypeScript:
```typescript
function connect(root: Node | null): Node | null {
let helperQueue: Node[] = [];
let preNode: Node, curNode: Node;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
for (let i = 0, length = helperQueue.length; i < length; i++) {
if (i === 0) {
preNode = helperQueue.shift()!;
} else {
curNode = helperQueue.shift()!;
preNode.next = curNode;
preNode = curNode;
}
if (preNode.left) helperQueue.push(preNode.left);
if (preNode.right) helperQueue.push(preNode.right);
}
preNode.next = null;
}
return root;
};
```
go:
```GO
@ -1689,6 +1862,31 @@ var connect = function(root) {
return root;
};
```
TypeScript:
```typescript
function connect(root: Node | null): Node | null {
let helperQueue: Node[] = [];
let preNode: Node, curNode: Node;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
for (let i = 0, length = helperQueue.length; i < length; i++) {
if (i === 0) {
preNode = helperQueue.shift()!;
} else {
curNode = helperQueue.shift()!;
preNode.next = curNode;
preNode = curNode;
}
if (preNode.left) helperQueue.push(preNode.left);
if (preNode.right) helperQueue.push(preNode.right);
}
preNode.next = null;
}
return root;
};
```
go:
```GO
@ -1933,7 +2131,28 @@ var maxDepth = function(root) {
};
```
TypeScript:
```typescript
function maxDepth(root: TreeNode | null): number {
let helperQueue: TreeNode[] = [];
let resDepth: number = 0;
let tempNode: TreeNode;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
resDepth++;
for (let i = 0, length = helperQueue.length; i < length; i++) {
tempNode = helperQueue.shift()!;
if (tempNode.left) helperQueue.push(tempNode.left);
if (tempNode.right) helperQueue.push(tempNode.right);
}
}
return resDepth;
};
```
Swift:
```swift
func maxDepth(_ root: TreeNode?) -> Int {
guard let root = root else {
@ -2130,7 +2349,29 @@ var minDepth = function(root) {
};
```
TypeScript:
```typescript
function minDepth(root: TreeNode | null): number {
let helperQueue: TreeNode[] = [];
let resMin: number = 0;
let tempNode: TreeNode;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
resMin++;
for (let i = 0, length = helperQueue.length; i < length; i++) {
tempNode = helperQueue.shift()!;
if (tempNode.left === null && tempNode.right === null) return resMin;
if (tempNode.left !== null) helperQueue.push(tempNode.left);
if (tempNode.right !== null) helperQueue.push(tempNode.right);
}
}
return resMin;
};
```
Swift
```swift
func minDepth(_ root: TreeNode?) -> Int {
guard let root = root else {

View File

@ -598,7 +598,81 @@ var maxDepth = function(root) {
};
```
## TypeScript
> 二叉树的最大深度:
```typescript
// 后续遍历(自下而上)
function maxDepth(root: TreeNode | null): number {
if (root === null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
};
// 前序遍历(自上而下)
function maxDepth(root: TreeNode | null): number {
function recur(node: TreeNode | null, count: number) {
if (node === null) {
resMax = resMax > count ? resMax : count;
return;
}
recur(node.left, count + 1);
recur(node.right, count + 1);
}
let resMax: number = 0;
let count: number = 0;
recur(root, count);
return resMax;
};
// 层序遍历(迭代法)
function maxDepth(root: TreeNode | null): number {
let helperQueue: TreeNode[] = [];
let resDepth: number = 0;
let tempNode: TreeNode;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
resDepth++;
for (let i = 0, length = helperQueue.length; i < length; i++) {
tempNode = helperQueue.shift()!;
if (tempNode.left) helperQueue.push(tempNode.left);
if (tempNode.right) helperQueue.push(tempNode.right);
}
}
return resDepth;
};
```
> N叉树的最大深度
```typescript
// 后续遍历(自下而上)
function maxDepth(root: TreeNode | null): number {
if (root === null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
};
// 前序遍历(自上而下)
function maxDepth(root: TreeNode | null): number {
function recur(node: TreeNode | null, count: number) {
if (node === null) {
resMax = resMax > count ? resMax : count;
return;
}
recur(node.left, count + 1);
recur(node.right, count + 1);
}
let resMax: number = 0;
let count: number = 0;
recur(root, count);
return resMax;
};
```
## C
二叉树最大深度递归
```c
int maxDepth(struct TreeNode* root){

View File

@ -604,7 +604,8 @@ func abs(a int)int{
}
```
## JavaScript
## JavaScript
递归法:
```javascript
var isBalanced = function(root) {
//还是用递归三部曲 + 后序遍历 左右中 当前左子树右子树高度相差大于1就返回-1
@ -614,8 +615,10 @@ var isBalanced = function(root) {
if(node === null) return 0;
// 3. 确定单层递归逻辑
let leftDepth = getDepth(node.left); //左子树高度
let rightDepth = getDepth(node.right); //右子树高度
// 当判定左子树不为平衡二叉树时,即可直接返回-1
if(leftDepth === -1) return -1;
let rightDepth = getDepth(node.right); //右子树高度
// 当判定右子树不为平衡二叉树时,即可直接返回-1
if(rightDepth === -1) return -1;
if(Math.abs(leftDepth - rightDepth) > 1) {
return -1;
@ -627,7 +630,68 @@ var isBalanced = function(root) {
};
```
迭代法:
```javascript
// 获取当前节点的高度
var getHeight = function (curNode) {
let queue = [];
if (curNode !== null) queue.push(curNode); // 压入当前元素
let depth = 0, res = 0;
while (queue.length) {
let node = queue[queue.length - 1]; // 取出栈顶
if (node !== null) {
queue.pop();
queue.push(node); // 中
queue.push(null);
depth++;
node.right && queue.push(node.right); // 右
node.left && queue.push(node.left); // 左
} else {
queue.pop();
node = queue[queue.length - 1];
queue.pop();
depth--;
}
res = res > depth ? res : depth;
}
return res;
}
var isBalanced = function (root) {
if (root === null) return true;
let queue = [root];
while (queue.length) {
let node = queue[queue.length - 1]; // 取出栈顶
queue.pop();
if (Math.abs(getHeight(node.left) - getHeight(node.right)) > 1) {
return false;
}
node.right && queue.push(node.right);
node.left && queue.push(node.left);
}
return true;
};
```
## TypeScript
```typescript
// 递归法
function isBalanced(root: TreeNode | null): boolean {
function getDepth(root: TreeNode | null): number {
if (root === null) return 0;
let leftDepth: number = getDepth(root.left);
if (leftDepth === -1) return -1;
let rightDepth: number = getDepth(root.right);
if (rightDepth === -1) return -1;
if (Math.abs(leftDepth - rightDepth) > 1) return -1;
return 1 + Math.max(leftDepth, rightDepth);
}
return getDepth(root) !== -1;
};
```
## C
递归法:
```c
int getDepth(struct TreeNode* node) {

View File

@ -404,6 +404,44 @@ var minDepth = function(root) {
};
```
## TypeScript
> 递归法
```typescript
function minDepth(root: TreeNode | null): number {
if (root === null) return 0;
if (root.left !== null && root.right === null) {
return 1 + minDepth(root.left);
}
if (root.left === null && root.right !== null) {
return 1 + minDepth(root.right);
}
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}
```
> 迭代法
```typescript
function minDepth(root: TreeNode | null): number {
let helperQueue: TreeNode[] = [];
let resMin: number = 0;
let tempNode: TreeNode;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
resMin++;
for (let i = 0, length = helperQueue.length; i < length; i++) {
tempNode = helperQueue.shift()!;
if (tempNode.left === null && tempNode.right === null) return resMin;
if (tempNode.left !== null) helperQueue.push(tempNode.left);
if (tempNode.right !== null) helperQueue.push(tempNode.right);
}
}
return resMin;
};
```
## Swift
> 递归

View File

@ -531,82 +531,63 @@ class solution:
```go
//递归法
/**
* definition for a binary tree node.
* type treenode struct {
* val int
* left *treenode
* right *treenode
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func haspathsum(root *treenode, targetsum int) bool {
var flage bool //找没找到的标志
if root==nil{
return flage
func hasPathSum(root *TreeNode, targetSum int) bool {
if root == nil {
return false
}
pathsum(root,0,targetsum,&flage)
return flage
}
func pathsum(root *treenode, sum int,targetsum int,flage *bool){
sum+=root.val
if root.left==nil&&root.right==nil&&sum==targetsum{
*flage=true
return
}
if root.left!=nil&&!(*flage){//左节点不为空且还没找到
pathsum(root.left,sum,targetsum,flage)
}
if root.right!=nil&&!(*flage){//右节点不为空且没找到
pathsum(root.right,sum,targetsum,flage)
targetSum -= root.Val // 将targetSum在遍历每层的时候都减去本层节点的值
if root.Left == nil && root.Right == nil && targetSum == 0 { // 如果剩余的targetSum为0, 则正好就是符合的结果
return true
}
return hasPathSum(root.Left, targetSum) || hasPathSum(root.Right, targetSum) // 否则递归找
}
```
113 递归法
113. 路径总和 II
```go
/**
* definition for a binary tree node.
* type treenode struct {
* val int
* left *treenode
* right *treenode
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func pathsum(root *treenode, targetsum int) [][]int {
var result [][]int//最终结果
if root==nil{
return result
}
var sumnodes []int//经过路径的节点集合
haspathsum(root,&sumnodes,targetsum,&result)
func pathSum(root *TreeNode, targetSum int) [][]int {
result := make([][]int, 0)
traverse(root, &result, new([]int), targetSum)
return result
}
func haspathsum(root *treenode,sumnodes *[]int,targetsum int,result *[][]int){
*sumnodes=append(*sumnodes,root.val)
if root.left==nil&&root.right==nil{//叶子节点
fmt.println(*sumnodes)
var sum int
var number int
for k,v:=range *sumnodes{//求该路径节点的和
sum+=v
number=k
}
tempnodes:=make([]int,number+1)//新的nodes接受指针里的值防止最终指针里的值发生变动导致最后的结果都是最后一个sumnodes的值
for k,v:=range *sumnodes{
tempnodes[k]=v
}
if sum==targetsum{
*result=append(*result,tempnodes)
}
func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) {
if node == nil { // 这个判空也可以挪到递归遍历左右子树时去判断
return
}
if root.left!=nil{
haspathsum(root.left,sumnodes,targetsum,result)
*sumnodes=(*sumnodes)[:len(*sumnodes)-1]//回溯
}
if root.right!=nil{
haspathsum(root.right,sumnodes,targetsum,result)
*sumnodes=(*sumnodes)[:len(*sumnodes)-1]//回溯
targetSum -= node.Val // 将targetSum在遍历每层的时候都减去本层节点的值
*currPath = append(*currPath, node.Val) // 把当前节点放到路径记录里
if node.Left == nil && node.Right == nil && targetSum == 0 { // 如果剩余的targetSum为0, 则正好就是符合的结果
// 不能直接将currPath放到result里面, 因为currPath是共享的, 每次遍历子树时都会被修改
pathCopy := make([]int, len(*currPath))
for i, element := range *currPath {
pathCopy[i] = element
}
*result = append(*result, pathCopy) // 将副本放到结果集里
}
traverse(node.Left, result, currPath, targetSum)
traverse(node.Right, result, currPath, targetSum)
*currPath = (*currPath)[:len(*currPath)-1] // 当前节点遍历完成, 从路径记录里删除掉
}
```

View File

@ -248,6 +248,36 @@ class Solution {
return valid[s.length()];
}
}
// 回溯法+记忆化
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> wordDictSet = new HashSet(wordDict);
int[] memory = new int[s.length()];
return backTrack(s, wordDictSet, 0, memory);
}
public boolean backTrack(String s, Set<String> wordDictSet, int startIndex, int[] memory) {
// 结束条件
if (startIndex >= s.length()) {
return true;
}
if (memory[startIndex] != 0) {
// 此处认为memory[i] = 1 表示可以拼出i 及以后的字符子串, memory[i] = -1 表示不能
return memory[startIndex] == 1 ? true : false;
}
for (int i = startIndex; i < s.length(); ++i) {
// 处理 递归 回溯 循环不变量:[startIndex, i + 1)
String word = s.substring(startIndex, i + 1);
if (wordDictSet.contains(word) && backTrack(s, wordDictSet, i + 1, memory)) {
memory[startIndex] = 1;
return true;
}
}
memory[startIndex] = -1;
return false;
}
}
```
Python

View File

@ -447,7 +447,63 @@ var countNodes = function(root) {
};
```
## TypeScrpt:
> 递归法
```typescript
function countNodes(root: TreeNode | null): number {
if (root === null) return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
};
```
> 迭代法
```typescript
function countNodes(root: TreeNode | null): number {
let helperQueue: TreeNode[] = [];
let resCount: number = 0;
let tempNode: TreeNode;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
for (let i = 0, length = helperQueue.length; i < length; i++) {
tempNode = helperQueue.shift()!;
resCount++;
if (tempNode.left) helperQueue.push(tempNode.left);
if (tempNode.right) helperQueue.push(tempNode.right);
}
}
return resCount;
};
```
> 利用完全二叉树性质
```typescript
function countNodes(root: TreeNode | null): number {
if (root === null) return 0;
let left: number = 0,
right: number = 0;
let curNode: TreeNode | null= root;
while (curNode !== null) {
left++;
curNode = curNode.left;
}
curNode = root;
while (curNode !== null) {
right++;
curNode = curNode.right;
}
if (left === right) {
return 2 ** left - 1;
}
return 1 + countNodes(root.left) + countNodes(root.right);
};
```
## C:
递归法
```c
int countNodes(struct TreeNode* root) {
@ -538,7 +594,7 @@ func _countNodes(_ root: TreeNode?) -> Int {
return 1 + leftCount + rightCount
}
```
> 层序遍历
```Swift
func countNodes(_ root: TreeNode?) -> Int {
@ -564,7 +620,7 @@ func countNodes(_ root: TreeNode?) -> Int {
return res
}
```
> 利用完全二叉树性质
```Swift
func countNodes(_ root: TreeNode?) -> Int {

View File

@ -563,7 +563,135 @@ var invertTree = function(root) {
};
```
### TypeScript
递归法:
```typescript
// 递归法(前序遍历)
function invertTree(root: TreeNode | null): TreeNode | null {
if (root === null) return root;
let tempNode: TreeNode | null = root.left;
root.left = root.right;
root.right = tempNode;
invertTree(root.left);
invertTree(root.right);
return root;
};
// 递归法(后序遍历)
function invertTree(root: TreeNode | null): TreeNode | null {
if (root === null) return root;
invertTree(root.left);
invertTree(root.right);
let tempNode: TreeNode | null = root.left;
root.left = root.right;
root.right = tempNode;
return root;
};
// 递归法(中序遍历)
function invertTree(root: TreeNode | null): TreeNode | null {
if (root === null) return root;
invertTree(root.left);
let tempNode: TreeNode | null = root.left;
root.left = root.right;
root.right = tempNode;
// 因为左右节点已经进行交换此时的root.left 是原先的root.right
invertTree(root.left);
return root;
};
```
迭代法:
```typescript
// 迭代法(栈模拟前序遍历)
function invertTree(root: TreeNode | null): TreeNode | null {
let helperStack: TreeNode[] = [];
let curNode: TreeNode,
tempNode: TreeNode | null;
if (root !== null) helperStack.push(root);
while (helperStack.length > 0) {
curNode = helperStack.pop()!;
// 入栈操作最好在交换节点之前进行,便于理解
if (curNode.right) helperStack.push(curNode.right);
if (curNode.left) helperStack.push(curNode.left);
tempNode = curNode.left;
curNode.left = curNode.right;
curNode.right = tempNode;
}
return root;
};
// 迭代法(栈模拟中序遍历-统一写法形式)
function invertTree(root: TreeNode | null): TreeNode | null {
let helperStack: (TreeNode | null)[] = [];
let curNode: TreeNode | null,
tempNode: TreeNode | null;
if (root !== null) helperStack.push(root);
while (helperStack.length > 0) {
curNode = helperStack.pop();
if (curNode !== null) {
if (curNode.right !== null) helperStack.push(curNode.right);
helperStack.push(curNode);
helperStack.push(null);
if (curNode.left !== null) helperStack.push(curNode.left);
} else {
curNode = helperStack.pop()!;
tempNode = curNode.left;
curNode.left = curNode.right;
curNode.right = tempNode;
}
}
return root;
};
// 迭代法(栈模拟后序遍历-统一写法形式)
function invertTree(root: TreeNode | null): TreeNode | null {
let helperStack: (TreeNode | null)[] = [];
let curNode: TreeNode | null,
tempNode: TreeNode | null;
if (root !== null) helperStack.push(root);
while (helperStack.length > 0) {
curNode = helperStack.pop();
if (curNode !== null) {
helperStack.push(curNode);
helperStack.push(null);
if (curNode.right !== null) helperStack.push(curNode.right);
if (curNode.left !== null) helperStack.push(curNode.left);
} else {
curNode = helperStack.pop()!;
tempNode = curNode.left;
curNode.left = curNode.right;
curNode.right = tempNode;
}
}
return root;
};
// 迭代法(队列模拟层序遍历)
function invertTree(root: TreeNode | null): TreeNode | null {
const helperQueue: TreeNode[] = [];
let curNode: TreeNode,
tempNode: TreeNode | null;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
for (let i = 0, length = helperQueue.length; i < length; i++) {
curNode = helperQueue.shift()!;
tempNode = curNode.left;
curNode.left = curNode.right;
curNode.right = tempNode;
if (curNode.left !== null) helperQueue.push(curNode.left);
if (curNode.right !== null) helperQueue.push(curNode.right);
}
}
return root;
};
```
### C:
递归法
```c
struct TreeNode* invertTree(struct TreeNode* root){

View File

@ -45,7 +45,7 @@
这个队列应该长这个样子:
```
```cpp
class MyQueue {
public:
void pop(int value) {
@ -597,5 +597,39 @@ class Solution {
}
```
Swift解法二
```swift
func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
var result = [Int]()
var window = [Int]()
var right = 0, left = right - k + 1
while right < nums.count {
let value = nums[right]
// 因为窗口移动丢弃的左边数
if left > 0, left - 1 == window.first {
window.removeFirst()
}
// 保证末尾的是最大的
while !window.isEmpty, value > nums[window.last!] {
window.removeLast()
}
window.append(right)
if left >= 0 { // 窗口形成
result.append(nums[window.first!])
}
right += 1
left += 1
}
return result
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -174,7 +174,7 @@ public:
```Java
class Solution {
public int wiggleMaxLength(int[] nums) {
if (nums == null || nums.length <= 1) {
if (nums.length <= 1) {
return nums.length;
}
//当前差值

View File

@ -210,6 +210,45 @@ class Solution {
}
```
二维数组版本(易于理解):
```Java
class Solution {
public boolean canPartition(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
if (sum % 2 == 1)
return false;
int target = sum / 2;
//dp[i][j]代表可装物品为0-i背包容量为j的情况下背包内容量的最大价值
int[][] dp = new int[nums.length][target + 1];
//初始化,dp[0][j]的最大价值nums[0](if j > weight[i])
//dp[i][0]均为0不用初始化
for (int j = nums[0]; j <= target; j++) {
dp[0][j] = nums[0];
}
//遍历物品,遍历背包
//递推公式:
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j <= target; j++) {
//背包容量可以容纳nums[i]
if (j >= nums[i]) {
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[nums.length - 1][target] == target;
}
}
```
Python
```python
class Solution:

View File

@ -272,7 +272,8 @@ Python
class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
sumValue = sum(nums)
if target > sumValue or (sumValue + target) % 2 == 1: return 0
#注意边界条件为 target>sumValue or target<-sumValue or (sumValue + target) % 2 == 1
if abs(target) > sumValue or (sumValue + target) % 2 == 1: return 0
bagSize = (sumValue + target) // 2
dp = [0] * (bagSize + 1)
dp[0] = 1

View File

@ -346,14 +346,12 @@ char * removeDuplicates(char * s){
Swift
```swift
func removeDuplicates(_ s: String) -> String {
let array = Array(s)
var stack = [Character]()
for c in array {
let last: Character? = stack.last
if stack.isEmpty || last != c {
stack.append(c)
} else {
for c in s {
if stack.last == c {
stack.removeLast()
} else {
stack.append(c)
}
}
return String(stack)

View File

@ -153,6 +153,8 @@ public:
Java
一维数组版本
```Java
class Solution {
public int lastStoneWeightII(int[] stones) {
@ -173,6 +175,41 @@ class Solution {
return sum - 2 * dp[target];
}
}
```
二维数组版本(便于理解)
```Java
class Solution {
public int lastStoneWeightII(int[] stones) {
int sum = 0;
for (int s : stones) {
sum += s;
}
int target = sum / 2;
//初始化dp[i][j]为可以放0-i物品背包容量为j的情况下背包中的最大价值
int[][] dp = new int[stones.length][target + 1];
//dp[i][0]默认初始化为0
//dp[0][j]取决于stones[0]
for (int j = stones[0]; j <= target; j++) {
dp[0][j] = stones[0];
}
for (int i = 1; i < stones.length; i++) {
for (int j = 1; j <= target; j++) {//注意是等于
if (j >= stones[i]) {
//不放:dp[i - 1][j] 放:dp[i - 1][j - stones[i]] + stones[i]
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - stones[i]] + stones[i]);
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
System.out.println(dp[stones.length - 1][target]);
return (sum - dp[stones.length - 1][target]) - dp[stones.length - 1][target];
}
}
```
Python

View File

@ -154,7 +154,7 @@
C++代码如下:
```
```cpp
struct TreeNode {
int val;
TreeNode *left;
@ -163,7 +163,7 @@ struct TreeNode {
};
```
大家会发现二叉树的定义 和链表是差不多的,相对于链表 ,二叉树的节点里多了一个指针, 有两个指针,指向左右孩子.
大家会发现二叉树的定义 和链表是差不多的,相对于链表 ,二叉树的节点里多了一个指针, 有两个指针,指向左右孩子
这里要提醒大家要注意二叉树节点定义的书写方式。
@ -177,7 +177,7 @@ struct TreeNode {
本篇我们介绍了二叉树的种类、存储方式、遍历方式以及定义,比较全面的介绍了二叉树各个方面的重点,帮助大家扫一遍基础。
**说二叉树,就不得不说递归,很多同学对递归都是又熟悉又陌生,递归的代码一般很简短,但每次都是一看就会,一写就废。**
**说二叉树,就不得不说递归,很多同学对递归都是又熟悉又陌生,递归的代码一般很简短,但每次都是一看就会,一写就废。**
## 其他语言版本

View File

@ -522,7 +522,75 @@ var postorderTraversal = function(root, res = []) {
```
TypeScript
```typescript
// 前序遍历(迭代法)
function preorderTraversal(root: TreeNode | null): number[] {
let helperStack: (TreeNode | null)[] = [];
let res: number[] = [];
let curNode: TreeNode | null;
if (root === null) return res;
helperStack.push(root);
while (helperStack.length > 0) {
curNode = helperStack.pop()!;
if (curNode !== null) {
if (curNode.right !== null) helperStack.push(curNode.right);
helperStack.push(curNode);
helperStack.push(null);
if (curNode.left !== null) helperStack.push(curNode.left);
} else {
curNode = helperStack.pop()!;
res.push(curNode.val);
}
}
return res;
};
// 中序遍历(迭代法)
function inorderTraversal(root: TreeNode | null): number[] {
let helperStack: (TreeNode | null)[] = [];
let res: number[] = [];
let curNode: TreeNode | null;
if (root === null) return res;
helperStack.push(root);
while (helperStack.length > 0) {
curNode = helperStack.pop()!;
if (curNode !== null) {
if (curNode.right !== null) helperStack.push(curNode.right);
helperStack.push(curNode);
helperStack.push(null);
if (curNode.left !== null) helperStack.push(curNode.left);
} else {
curNode = helperStack.pop()!;
res.push(curNode.val);
}
}
return res;
};
// 后序遍历(迭代法)
function postorderTraversal(root: TreeNode | null): number[] {
let helperStack: (TreeNode | null)[] = [];
let res: number[] = [];
let curNode: TreeNode | null;
if (root === null) return res;
helperStack.push(root);
while (helperStack.length > 0) {
curNode = helperStack.pop()!;
if (curNode !== null) {
if (curNode.right !== null) helperStack.push(curNode.right);
helperStack.push(curNode);
helperStack.push(null);
if (curNode.left !== null) helperStack.push(curNode.left);
} else {
curNode = helperStack.pop()!;
res.push(curNode.val);
}
}
return res;
};
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -251,7 +251,4 @@ int main() {
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -218,7 +218,4 @@ leetcode是专门针对算法练习的题库leetcode现在也推出了中文
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -280,7 +280,4 @@ public class TimeComplexity {
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -130,7 +130,4 @@
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -119,7 +119,4 @@ int main() {
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -170,7 +170,4 @@ $O(2 × n^2 + 10 × n + 1000) < O(3 × n^2)$,所以说最后省略掉常数项
-----------------------
* 作者微信[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -73,7 +73,4 @@ for (int i = 0; i < n; i++) {
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -150,7 +150,4 @@ char型的数据和int型的数据挨在一起该int数据从地址1开始
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -67,7 +67,4 @@ int main() {
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -116,7 +116,4 @@
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -79,7 +79,4 @@
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -77,7 +77,4 @@
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -87,7 +87,4 @@
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -82,7 +82,4 @@
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -136,7 +136,4 @@ Markdown支持部分html例如这样
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -133,7 +133,4 @@ Carl校招社招都拿过大厂的offer同时也看过很多应聘者的简
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -269,7 +269,4 @@ int binary_search( int arr[], int l, int r, int x) {
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -152,7 +152,4 @@ int function3(int x, int n) {
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>

View File

@ -158,22 +158,5 @@ cd a/b/c/../../
好了栈与队列我们就总结到这里了接下来Carl就要带大家开启新的篇章了大家加油
## 其他语言版本
Java
Python
Go
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -224,12 +224,14 @@ var getIntersectionNode = function(headA, headB) {
lenA = getListLen(headA),
lenB = getListLen(headB);
if(lenA < lenB) {
// 下面交换变量注意加 “分号” ,两个数组交换变量在同一个作用域下时
// 如果不加分号,下面两条代码等同于一条代码: [curA, curB] = [lenB, lenA]
[curA, curB] = [curB, curA];
[lenA, lenB] = [lenB, lenA];
}
let i = lenA - lenB;
while(i-- > 0) {
curA = curA.next
curA = curA.next;
}
while(curA && curA !== curB) {
curA = curA.next;