Skip to content

Commit dafd02d

Browse files
authored
feat: update lc problem: No.2715 (#1523)
1 parent a34635a commit dafd02d

File tree

6 files changed

+24
-54
lines changed

6 files changed

+24
-54
lines changed

solution/2700-2799/2715.Timeout Cancellation/README.md

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [2715. Timeout Cancellation](https://leetcode.cn/problems/timeout-cancellation)
1+
# [2715. 执行可取消的延迟函数](https://leetcode.cn/problems/execute-cancellable-function-with-delay)
22

33
[English Version](/solution/2700-2799/2715.Timeout%20Cancellation/README_EN.md)
44

@@ -69,16 +69,10 @@ setTimeout(cancel, 100);
6969

7070
```ts
7171
function cancellable(fn: Function, args: any[], t: number): Function {
72-
let cancelled = false;
73-
const cancel = () => {
74-
cancelled = true;
72+
const timer = setTimeout(() => fn(...args), t);
73+
return () => {
74+
clearTimeout(timer);
7575
};
76-
setTimeout(() => {
77-
if (!cancelled) {
78-
fn(...args);
79-
}
80-
}, t);
81-
return cancel;
8276
}
8377

8478
/**
@@ -118,14 +112,10 @@ function cancellable(fn: Function, args: any[], t: number): Function {
118112
* @return {Function}
119113
*/
120114
var cancellable = function (fn, args, t) {
121-
let cancelled = false;
122-
const calcel = () => (cancelled = true);
123-
setTimeout(() => {
124-
if (!cancelled) {
125-
fn(...args);
126-
}
127-
}, t);
128-
return calcel;
115+
const timer = setTimeout(() => fn(...args), t);
116+
return () => {
117+
clearTimeout(timer);
118+
};
129119
};
130120

131121
/**

solution/2700-2799/2715.Timeout Cancellation/README_EN.md

+7-17
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,10 @@ The cancellation was scheduled to occur after a delay of cancelT (100ms), which
6464

6565
```ts
6666
function cancellable(fn: Function, args: any[], t: number): Function {
67-
let cancelled = false;
68-
const cancel = () => {
69-
cancelled = true;
67+
const timer = setTimeout(() => fn(...args), t);
68+
return () => {
69+
clearTimeout(timer);
7070
};
71-
setTimeout(() => {
72-
if (!cancelled) {
73-
fn(...args);
74-
}
75-
}, t);
76-
return cancel;
7771
}
7872

7973
/**
@@ -113,14 +107,10 @@ function cancellable(fn: Function, args: any[], t: number): Function {
113107
* @return {Function}
114108
*/
115109
var cancellable = function (fn, args, t) {
116-
let cancelled = false;
117-
const calcel = () => (cancelled = true);
118-
setTimeout(() => {
119-
if (!cancelled) {
120-
fn(...args);
121-
}
122-
}, t);
123-
return calcel;
110+
const timer = setTimeout(() => fn(...args), t);
111+
return () => {
112+
clearTimeout(timer);
113+
};
124114
};
125115

126116
/**

solution/2700-2799/2715.Timeout Cancellation/Solution.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
* @return {Function}
66
*/
77
var cancellable = function (fn, args, t) {
8-
let cancelled = false;
9-
const calcel = () => (cancelled = true);
10-
setTimeout(() => {
11-
if (!cancelled) {
12-
fn(...args);
13-
}
14-
}, t);
15-
return calcel;
8+
const timer = setTimeout(() => fn(...args), t);
9+
return () => {
10+
clearTimeout(timer);
11+
};
1612
};
1713

1814
/**

solution/2700-2799/2715.Timeout Cancellation/Solution.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
function cancellable(fn: Function, args: any[], t: number): Function {
2-
let cancelled = false;
3-
const cancel = () => {
4-
cancelled = true;
2+
const timer = setTimeout(() => fn(...args), t);
3+
return () => {
4+
clearTimeout(timer);
55
};
6-
setTimeout(() => {
7-
if (!cancelled) {
8-
fn(...args);
9-
}
10-
}, t);
11-
return cancel;
126
}
137

148
/**

solution/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2725,7 +2725,7 @@
27252725
| 2712 | [使所有字符相等的最小成本](/solution/2700-2799/2712.Minimum%20Cost%20to%20Make%20All%20Characters%20Equal/README.md) | `贪心`,`字符串`,`动态规划` | 中等 | 第 347 场周赛 |
27262726
| 2713 | [矩阵中严格递增的单元格数](/solution/2700-2799/2713.Maximum%20Strictly%20Increasing%20Cells%20in%20a%20Matrix/README.md) | `记忆化搜索`,`数组`,`二分查找`,`动态规划`,`矩阵`,`排序` | 困难 | 第 347 场周赛 |
27272727
| 2714 | [找到最短路径的 K 次跨越](/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/README.md) | `图`,`最短路`,`堆(优先队列)` | 困难 | 🔒 |
2728-
| 2715 | [Timeout Cancellation](/solution/2700-2799/2715.Timeout%20Cancellation/README.md) | | 简单 | |
2728+
| 2715 | [执行可取消的延迟函数](/solution/2700-2799/2715.Timeout%20Cancellation/README.md) | | 简单 | |
27292729
| 2716 | [最小化字符串长度](/solution/2700-2799/2716.Minimize%20String%20Length/README.md) | `哈希表`,`字符串` | 简单 | 第 348 场周赛 |
27302730
| 2717 | [半有序排列](/solution/2700-2799/2717.Semi-Ordered%20Permutation/README.md) | `数组`,`模拟` | 简单 | 第 348 场周赛 |
27312731
| 2718 | [查询后矩阵的和](/solution/2700-2799/2718.Sum%20of%20Matrix%20After%20Queries/README.md) | `数组`,`哈希表` | 中等 | 第 348 场周赛 |

solution/summary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2768,7 +2768,7 @@
27682768
- [2712.使所有字符相等的最小成本](/solution/2700-2799/2712.Minimum%20Cost%20to%20Make%20All%20Characters%20Equal/README.md)
27692769
- [2713.矩阵中严格递增的单元格数](/solution/2700-2799/2713.Maximum%20Strictly%20Increasing%20Cells%20in%20a%20Matrix/README.md)
27702770
- [2714.找到最短路径的 K 次跨越](/solution/2700-2799/2714.Find%20Shortest%20Path%20with%20K%20Hops/README.md)
2771-
- [2715.Timeout Cancellation](/solution/2700-2799/2715.Timeout%20Cancellation/README.md)
2771+
- [2715.执行可取消的延迟函数](/solution/2700-2799/2715.Timeout%20Cancellation/README.md)
27722772
- [2716.最小化字符串长度](/solution/2700-2799/2716.Minimize%20String%20Length/README.md)
27732773
- [2717.半有序排列](/solution/2700-2799/2717.Semi-Ordered%20Permutation/README.md)
27742774
- [2718.查询后矩阵的和](/solution/2700-2799/2718.Sum%20of%20Matrix%20After%20Queries/README.md)

0 commit comments

Comments
 (0)