Skip to content

Commit a0de44d

Browse files
author
guimeisang
committed
Merge branch 'master' of github.com:guimeisang/algorithm
2 parents a0aef3a + 716d16c commit a0de44d

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

src/202308/arr/接雨水.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// 给定[0,1,0,2,1,0,1,3,2,1,2,1]
2+
3+
function rain(height) {
4+
let left = 0, right = height.length - 1
5+
let res = 0, leftMax = 0, rightMax = 0
6+
while(left < right) {
7+
leftMax = Math.max(leftMax, height[left])
8+
rightMax = Math.max(rightMax, height[right])
9+
if(height[left] < height[right]) {
10+
res += leftMax - height[left]
11+
left++
12+
}else {
13+
res += rightMax - height[right]
14+
right--
15+
}
16+
}
17+
return res
18+
}
19+
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+
62+
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

src/202308/func/手写reduce.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Array.prototype.myReduce = function(callback, initialValue) {
2+
let accumulator = initialValue === undefined ? undefined : initialValue;
3+
for (let i = 0; i < this.length; i++) {
4+
if(accumulator === undefined) {
5+
accumulator = this[i]
6+
}else {
7+
accumulator = callback(accumulator, this[i], i, this)
8+
}
9+
}
10+
return accumulator
11+
}

0 commit comments

Comments
 (0)