Skip to content

Commit 297dd96

Browse files
committed
feat: add solutions to lc problems: No.2626,2627
* No.2626.Array Reduce Transformation * No.2627.Debounce
1 parent 3a6772c commit 297dd96

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

solution/2600-2699/2626.Array Reduce Transformation/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ init = 25
8282
<!-- 这里可写当前语言的特殊实现逻辑 -->
8383

8484
```ts
85-
85+
type Fn = (accum: number, curr: number) => number;
86+
87+
function reduce(nums: number[], fn: Fn, init: number): number {
88+
let acc: number = init;
89+
for (const x of nums) {
90+
acc = fn(acc, x);
91+
}
92+
return acc;
93+
}
8694
```
8795

8896
### **...**

solution/2600-2699/2626.Array Reduce Transformation/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ init = 25
7474
### **TypeScript**
7575

7676
```ts
77-
77+
type Fn = (accum: number, curr: number) => number;
78+
79+
function reduce(nums: number[], fn: Fn, init: number): number {
80+
let acc: number = init;
81+
for (const x of nums) {
82+
acc = fn(acc, x);
83+
}
84+
return acc;
85+
}
7886
```
7987

8088
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Fn = (accum: number, curr: number) => number;
2+
3+
function reduce(nums: number[], fn: Fn, init: number): number {
4+
let acc: number = init;
5+
for (const x of nums) {
6+
acc = fn(acc, x);
7+
}
8+
return acc;
9+
}

solution/2600-2699/2627.Debounce/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,27 @@ calls = [
9797
<!-- 这里可写当前语言的特殊实现逻辑 -->
9898

9999
```ts
100+
type F = (...p: any[]) => any;
101+
102+
function debounce(fn: F, t: number): F {
103+
let timeout: ReturnType<typeof setTimeout> | undefined;
104+
105+
return function (...args) {
106+
if (timeout !== undefined) {
107+
clearTimeout(timeout);
108+
}
109+
timeout = setTimeout(() => {
110+
fn.apply(this, args);
111+
}, t);
112+
};
113+
}
100114

115+
/**
116+
* const log = debounce(console.log, 100);
117+
* log('Hello'); // cancelled
118+
* log('Hello'); // cancelled
119+
* log('Hello'); // Logged at t=100ms
120+
*/
101121
```
102122

103123
### **...**

solution/2600-2699/2627.Debounce/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,27 @@ The 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6).
8989
### **TypeScript**
9090

9191
```ts
92+
type F = (...p: any[]) => any;
93+
94+
function debounce(fn: F, t: number): F {
95+
let timeout: ReturnType<typeof setTimeout> | undefined;
96+
97+
return function (...args) {
98+
if (timeout !== undefined) {
99+
clearTimeout(timeout);
100+
}
101+
timeout = setTimeout(() => {
102+
fn.apply(this, args);
103+
}, t);
104+
};
105+
}
92106

107+
/**
108+
* const log = debounce(console.log, 100);
109+
* log('Hello'); // cancelled
110+
* log('Hello'); // cancelled
111+
* log('Hello'); // Logged at t=100ms
112+
*/
93113
```
94114

95115
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
type F = (...p: any[]) => any;
2+
3+
function debounce(fn: F, t: number): F {
4+
let timeout: ReturnType<typeof setTimeout> | undefined;
5+
6+
return function (...args) {
7+
if (timeout !== undefined) {
8+
clearTimeout(timeout);
9+
}
10+
timeout = setTimeout(() => {
11+
fn.apply(this, args);
12+
}, t);
13+
};
14+
}
15+
16+
/**
17+
* const log = debounce(console.log, 100);
18+
* log('Hello'); // cancelled
19+
* log('Hello'); // cancelled
20+
* log('Hello'); // Logged at t=100ms
21+
*/

0 commit comments

Comments
 (0)