Skip to content

Commit 716d16c

Browse files
add: 每天一练算法题,提高下编码速度
1 parent 9b3eac1 commit 716d16c

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

src/202308/arr/接雨水.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,62 @@
11
// 给定[0,1,0,2,1,0,1,3,2,1,2,1]
22

33
function rain(height) {
4-
let res = 0;
5-
let left = 0, right = height.length - 1;
6-
let leftMax = 0, rightMax = 0;
4+
let left = 0, right = height.length - 1
5+
let res = 0, leftMax = 0, rightMax = 0
76
while(left < right) {
7+
leftMax = Math.max(leftMax, height[left])
8+
rightMax = Math.max(rightMax, height[right])
89
if(height[left] < height[right]) {
9-
leftMax = Math.max(leftMax, height[left])
1010
res += leftMax - height[left]
11-
++left
11+
left++
1212
}else {
13-
rightMax = Math.max(rightMax, height[right])
1413
res += rightMax - height[right]
15-
--right
14+
right--
1615
}
1716
}
1817
return res
1918
}
2019

20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
// function rain(height) {
45+
// let leftIndex = 0, rightIndex = height.length - 1;
46+
// let leftMax = 0, rightMax = 0;
47+
// let res = 0
48+
// while(leftIndex < rightIndex) {
49+
// leftMax = Math.max(leftMax, height[leftIndex])
50+
// rightMax = Math.max(rightMax, height[rightIndex])
51+
// if(height[leftIndex] < height[rightIndex]) {
52+
// res += leftMax - height[leftIndex]
53+
// leftIndex++
54+
// }else {
55+
// res += rightMax - height[rightIndex]
56+
// rightIndex--
57+
// }
58+
// }
59+
// return res
60+
// }
61+
2162
console.log(rain([0,1,0,2,1,0,1,3,2,1,2,1]))

src/202308/func/addSumOf.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,43 @@
33
// Add(1,2)(3)(4).sumOf(); // 输出 10
44
// 涉及到的知识面有:闭包、递归、作用域、函数与对象
55
function Add() {
6-
if(!Add.nums) Add.nums = []
7-
Add.nums.push(...arguments)
6+
// 只处理参数
7+
if(!Add.arr) Add.arr = []
8+
console.log('看看参数:', ...arguments)
9+
Add.arr.push(...arguments)
810
return Add
911
}
1012

11-
Add.sumOf = () => {
12-
return Add.nums.reduce((a, b) => a + b)
13+
Add.sumOf = function() {
14+
return this.arr.reduce((a, b) => a + b)
1315
}
1416

15-
console.log(Add(1)(2)(3).sumOf())
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
// function Add() {
35+
// if(!Add.nums) Add.nums = []
36+
// Add.nums.push(...arguments)
37+
// return Add
38+
// }
39+
40+
// Add.sumOf = () => {
41+
// return Add.nums.reduce((a, b) => a + b)
42+
// }
43+
44+
console.log(Add(1,2)(3)(4).sumOf())
1645

0 commit comments

Comments
 (0)