Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 84335c5

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents cbaa9df + a46d696 commit 84335c5

8 files changed

+421
-6
lines changed

‎problems/0042.接雨水.md‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,91 @@ var trap = function(height) {
744744
};
745745
```
746746

747+
### TypeScript
748+
749+
双指针法:
750+
751+
```typescript
752+
function trap(height: number[]): number {
753+
const length: number = height.length;
754+
let resVal: number = 0;
755+
for (let i = 0; i < length; i++) {
756+
let leftMaxHeight: number = height[i],
757+
rightMaxHeight: number = height[i];
758+
let leftIndex: number = i - 1,
759+
rightIndex: number = i + 1;
760+
while (leftIndex >= 0) {
761+
if (height[leftIndex] > leftMaxHeight)
762+
leftMaxHeight = height[leftIndex];
763+
leftIndex--;
764+
}
765+
while (rightIndex < length) {
766+
if (height[rightIndex] > rightMaxHeight)
767+
rightMaxHeight = height[rightIndex];
768+
rightIndex++;
769+
}
770+
resVal += Math.min(leftMaxHeight, rightMaxHeight) - height[i];
771+
}
772+
return resVal;
773+
};
774+
```
775+
776+
动态规划:
777+
778+
```typescript
779+
function trap(height: number[]): number {
780+
const length: number = height.length;
781+
const leftMaxHeightDp: number[] = [],
782+
rightMaxHeightDp: number[] = [];
783+
leftMaxHeightDp[0] = height[0];
784+
rightMaxHeightDp[length - 1] = height[length - 1];
785+
for (let i = 1; i < length; i++) {
786+
leftMaxHeightDp[i] = Math.max(height[i], leftMaxHeightDp[i - 1]);
787+
}
788+
for (let i = length - 2; i >= 0; i--) {
789+
rightMaxHeightDp[i] = Math.max(height[i], rightMaxHeightDp[i + 1]);
790+
}
791+
let resVal: number = 0;
792+
for (let i = 0; i < length; i++) {
793+
resVal += Math.min(leftMaxHeightDp[i], rightMaxHeightDp[i]) - height[i];
794+
}
795+
return resVal;
796+
};
797+
```
798+
799+
单调栈:
800+
801+
```typescript
802+
function trap(height: number[]): number {
803+
const length: number = height.length;
804+
const stack: number[] = [];
805+
stack.push(0);
806+
let resVal: number = 0;
807+
for (let i = 1; i < length; i++) {
808+
let top = stack[stack.length - 1];
809+
if (height[top] > height[i]) {
810+
stack.push(i);
811+
} else if (height[top] === height[i]) {
812+
stack.pop();
813+
stack.push(i);
814+
} else {
815+
while (stack.length > 0 && height[top] < height[i]) {
816+
let mid = stack.pop();
817+
if (stack.length > 0) {
818+
let left = stack[stack.length - 1];
819+
let h = Math.min(height[left], height[i]) - height[mid];
820+
let w = i - left - 1;
821+
resVal += h * w;
822+
top = stack[stack.length - 1];
823+
}
824+
}
825+
stack.push(i);
826+
}
827+
}
828+
return resVal;
829+
};
830+
```
831+
747832
### C:
748833

749834
一种更简便的双指针方法:

‎problems/0084.柱状图中最大的矩形.md‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,5 +486,95 @@ var largestRectangleArea = function(heights) {
486486
return maxArea;
487487
};
488488
```
489+
TypeScript:
490+
491+
> 双指针法(会超时):
492+
493+
```typescript
494+
function largestRectangleArea(heights: number[]): number {
495+
let resMax: number = 0;
496+
for (let i = 0, length = heights.length; i < length; i++) {
497+
// 左开右开
498+
let left: number = i - 1,
499+
right: number = i + 1;
500+
while (left >= 0 && heights[left] >= heights[i]) {
501+
left--;
502+
}
503+
while (right < length && heights[right] >= heights[i]) {
504+
right++;
505+
}
506+
resMax = Math.max(resMax, heights[i] * (right - left - 1));
507+
}
508+
return resMax;
509+
};
510+
```
511+
512+
> 动态规划预处理:
513+
514+
```typescript
515+
function largestRectangleArea(heights: number[]): number {
516+
const length: number = heights.length;
517+
const leftHeightDp: number[] = [],
518+
rightHeightDp: number[] = [];
519+
leftHeightDp[0] = -1;
520+
rightHeightDp[length - 1] = length;
521+
for (let i = 1; i < length; i++) {
522+
let j = i - 1;
523+
while (j >= 0 && heights[i] <= heights[j]) {
524+
j = leftHeightDp[j];
525+
}
526+
leftHeightDp[i] = j;
527+
}
528+
for (let i = length - 2; i >= 0; i--) {
529+
let j = i + 1;
530+
while (j < length && heights[i] <= heights[j]) {
531+
j = rightHeightDp[j];
532+
}
533+
rightHeightDp[i] = j;
534+
}
535+
let resMax: number = 0;
536+
for (let i = 0; i < length; i++) {
537+
let area = heights[i] * (rightHeightDp[i] - leftHeightDp[i] - 1);
538+
resMax = Math.max(resMax, area);
539+
}
540+
return resMax;
541+
};
542+
```
543+
544+
> 单调栈:
545+
546+
```typescript
547+
function largestRectangleArea(heights: number[]): number {
548+
heights.push(0);
549+
const length: number = heights.length;
550+
// 栈底->栈顶:严格单调递增
551+
const stack: number[] = [];
552+
stack.push(0);
553+
let resMax: number = 0;
554+
for (let i = 1; i < length; i++) {
555+
let top = stack[stack.length - 1];
556+
if (heights[top] < heights[i]) {
557+
stack.push(i);
558+
} else if (heights[top] === heights[i]) {
559+
stack.pop();
560+
stack.push(i);
561+
} else {
562+
while (stack.length > 0 && heights[top] > heights[i]) {
563+
let mid = stack.pop();
564+
let left = stack.length > 0 ? stack[stack.length - 1] : -1;
565+
let w = i - left - 1;
566+
let h = heights[mid];
567+
resMax = Math.max(resMax, w * h);
568+
top = stack[stack.length - 1];
569+
}
570+
stack.push(i);
571+
}
572+
}
573+
return resMax;
574+
};
575+
```
576+
577+
578+
489579
-----------------------
490580
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

‎problems/0104.二叉树的最大深度.md‎

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ class solution:
495495

496496

497497
## go
498-
498+
### 104.二叉树的最大深度
499499
```go
500500
/**
501501
* definition for a binary tree node.
@@ -548,6 +548,8 @@ func maxdepth(root *treenode) int {
548548

549549
## javascript
550550

551+
### 104.二叉树的最大深度
552+
551553
```javascript
552554
var maxdepth = function(root) {
553555
if (root === null) return 0;
@@ -595,6 +597,8 @@ var maxDepth = function(root) {
595597
};
596598
```
597599

600+
### 559.n叉树的最大深度
601+
598602
N叉树的最大深度 递归写法
599603
```js
600604
var maxDepth = function(root) {
@@ -627,9 +631,9 @@ var maxDepth = function(root) {
627631
};
628632
```
629633

630-
## TypeScript:
634+
## TypeScript
631635

632-
> 二叉树的最大深度:
636+
### 104.二叉树的最大深度
633637

634638
```typescript
635639
// 后续遍历(自下而上)
@@ -672,7 +676,7 @@ function maxDepth(root: TreeNode | null): number {
672676
};
673677
```
674678

675-
> N叉树的最大深度
679+
### 559.n叉树的最大深度
676680

677681
```typescript
678682
// 后续遍历(自下而上)
@@ -702,6 +706,8 @@ function maxDepth(root: TreeNode | null): number {
702706

703707
## C
704708

709+
### 104.二叉树的最大深度
710+
705711
二叉树最大深度递归
706712
```c
707713
int maxDepth(struct TreeNode* root){
@@ -758,7 +764,8 @@ int maxDepth(struct TreeNode* root){
758764

759765
## Swift
760766

761-
>二叉树最大深度
767+
### 104.二叉树的最大深度
768+
762769
```swift
763770
// 递归 - 后序
764771
func maxDepth1(_ root: TreeNode?) -> Int {
@@ -797,7 +804,8 @@ func maxDepth(_ root: TreeNode?) -> Int {
797804
}
798805
```
799806

800-
>N叉树最大深度
807+
### 559.n叉树的最大深度
808+
801809
```swift
802810
// 递归
803811
func maxDepth(_ root: Node?) -> Int {
@@ -833,5 +841,84 @@ func maxDepth1(_ root: Node?) -> Int {
833841
}
834842
```
835843

844+
## Scala
845+
846+
### 104.二叉树的最大深度
847+
递归法:
848+
```scala
849+
object Solution {
850+
def maxDepth(root: TreeNode): Int = {
851+
def process(curNode: TreeNode): Int = {
852+
if (curNode == null) return 0
853+
// 递归左节点和右节点,返回最大的,最后+1
854+
math.max(process(curNode.left), process(curNode.right)) + 1
855+
}
856+
// 调用递归方法,return关键字可以省略
857+
process(root)
858+
}
859+
}
860+
```
861+
862+
迭代法:
863+
```scala
864+
object Solution {
865+
import scala.collection.mutable
866+
def maxDepth(root: TreeNode): Int = {
867+
var depth = 0
868+
if (root == null) return depth
869+
val queue = mutable.Queue[TreeNode]()
870+
queue.enqueue(root)
871+
while (!queue.isEmpty) {
872+
val len = queue.size
873+
for (i <- 0 until len) {
874+
val curNode = queue.dequeue()
875+
if (curNode.left != null) queue.enqueue(curNode.left)
876+
if (curNode.right != null) queue.enqueue(curNode.right)
877+
}
878+
depth += 1 // 只要有层次就+=1
879+
}
880+
depth
881+
}
882+
}
883+
```
884+
885+
### 559.n叉树的最大深度
886+
887+
递归法:
888+
```scala
889+
object Solution {
890+
def maxDepth(root: Node): Int = {
891+
if (root == null) return 0
892+
var depth = 0
893+
for (node <- root.children) {
894+
depth = math.max(depth, maxDepth(node))
895+
}
896+
depth + 1
897+
}
898+
}
899+
```
900+
901+
迭代法: (层序遍历)
902+
```scala
903+
object Solution {
904+
import scala.collection.mutable
905+
def maxDepth(root: Node): Int = {
906+
if (root == null) return 0
907+
var depth = 0
908+
val queue = mutable.Queue[Node]()
909+
queue.enqueue(root)
910+
while (!queue.isEmpty) {
911+
val len = queue.size
912+
depth += 1
913+
for (i <- 0 until len) {
914+
val curNode = queue.dequeue()
915+
for (node <- curNode.children) queue.enqueue(node)
916+
}
917+
}
918+
depth
919+
}
920+
}
921+
```
922+
836923
-----------------------
837924
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /