Skip to content

feat: update lc problem: No.2715 #1523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions solution/2700-2799/2715.Timeout Cancellation/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [2715. Timeout Cancellation](https://leetcode.cn/problems/timeout-cancellation)
# [2715. 执行可取消的延迟函数](https://leetcode.cn/problems/execute-cancellable-function-with-delay)

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

Expand Down Expand Up @@ -69,16 +69,10 @@ setTimeout(cancel, 100);

```ts
function cancellable(fn: Function, args: any[], t: number): Function {
let cancelled = false;
const cancel = () => {
cancelled = true;
const timer = setTimeout(() => fn(...args), t);
return () => {
clearTimeout(timer);
};
setTimeout(() => {
if (!cancelled) {
fn(...args);
}
}, t);
return cancel;
}

/**
Expand Down Expand Up @@ -118,14 +112,10 @@ function cancellable(fn: Function, args: any[], t: number): Function {
* @return {Function}
*/
var cancellable = function (fn, args, t) {
let cancelled = false;
const calcel = () => (cancelled = true);
setTimeout(() => {
if (!cancelled) {
fn(...args);
}
}, t);
return calcel;
const timer = setTimeout(() => fn(...args), t);
return () => {
clearTimeout(timer);
};
};

/**
Expand Down
24 changes: 7 additions & 17 deletions solution/2700-2799/2715.Timeout Cancellation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,10 @@ The cancellation was scheduled to occur after a delay of cancelT (100ms), which

```ts
function cancellable(fn: Function, args: any[], t: number): Function {
let cancelled = false;
const cancel = () => {
cancelled = true;
const timer = setTimeout(() => fn(...args), t);
return () => {
clearTimeout(timer);
};
setTimeout(() => {
if (!cancelled) {
fn(...args);
}
}, t);
return cancel;
}

/**
Expand Down Expand Up @@ -113,14 +107,10 @@ function cancellable(fn: Function, args: any[], t: number): Function {
* @return {Function}
*/
var cancellable = function (fn, args, t) {
let cancelled = false;
const calcel = () => (cancelled = true);
setTimeout(() => {
if (!cancelled) {
fn(...args);
}
}, t);
return calcel;
const timer = setTimeout(() => fn(...args), t);
return () => {
clearTimeout(timer);
};
};

/**
Expand Down
12 changes: 4 additions & 8 deletions solution/2700-2799/2715.Timeout Cancellation/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
* @return {Function}
*/
var cancellable = function (fn, args, t) {
let cancelled = false;
const calcel = () => (cancelled = true);
setTimeout(() => {
if (!cancelled) {
fn(...args);
}
}, t);
return calcel;
const timer = setTimeout(() => fn(...args), t);
return () => {
clearTimeout(timer);
};
};

/**
Expand Down
12 changes: 3 additions & 9 deletions solution/2700-2799/2715.Timeout Cancellation/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
function cancellable(fn: Function, args: any[], t: number): Function {
let cancelled = false;
const cancel = () => {
cancelled = true;
const timer = setTimeout(() => fn(...args), t);
return () => {
clearTimeout(timer);
};
setTimeout(() => {
if (!cancelled) {
fn(...args);
}
}, t);
return cancel;
}

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