Skip to content

File tree

10 files changed

+94
-30
lines changed

10 files changed

+94
-30
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ go get github.com/masx200/leetcode-test
152152

153153
<summary>展开查看</summary>
154154

155+
https://leetcode.cn/problems/counter-ii/
156+
157+
https://leetcode.cn/problems/flipping-an-image/
158+
159+
https://leetcode.cn/problems/allow-one-function-call/
160+
161+
https://leetcode.cn/problems/chunk-array
162+
163+
https://leetcode.cn/problems/create-hello-world-function
164+
155165
https://leetcode.cn/problems/gas-station
156166

157167
https://leetcode.cn/problems/distribute-coins-in-binary-tree

allow-one-function-call/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export type JSONValue = null | boolean | number | string | JSONValue[] | {
2+
[key: string]: JSONValue;
3+
};
4+
export type OnceFn = (...args: JSONValue[]) => JSONValue | undefined;
5+
export default function once(fn: OnceFn): OnceFn {
6+
let et = false;
7+
return function (...args) {
8+
if (et) return;
9+
et = true;
10+
return fn(...args);
11+
};
12+
}

cache.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if (import.meta.main) {
2222

2323
async function parallel_cache(
2424
entry_iter: AsyncIterableIterator<WalkEntry>,
25-
limiter: AsyncCurrentLimiter
25+
limiter: AsyncCurrentLimiter,
2626
) {
2727
const files: string[] = [];
2828

@@ -37,21 +37,20 @@ async function parallel_cache(
3737
limiter.run(
3838
function (stack: string[]) {
3939
return runDenocache(stack);
40-
}.bind(null, stack)
40+
}.bind(null, stack),
4141
)
42-
)
42+
),
4343
);
4444
}
4545
async function start() {
4646
const limiter = new AsyncLimiterClass(1);
4747
const args = parse(Deno.args);
4848
console.log(args);
49-
const skip =
50-
typeof args.skip === "string"
51-
? new RegExp(String(args.skip))
52-
: Array.isArray(args.skip)
53-
? args.skip.map((s: string | RegExp) => new RegExp(s))
54-
: undefined;
49+
const skip = typeof args.skip === "string"
50+
? new RegExp(String(args.skip))
51+
: Array.isArray(args.skip)
52+
? args.skip.map((s: string | RegExp) => new RegExp(s))
53+
: undefined;
5554
const entry_iter = searchFilesNames({ skip });
5655
await parallel_cache(entry_iter, limiter);
5756
}
@@ -73,7 +72,7 @@ async function runDenocache(stack: string[]) {
7372
if (!success) {
7473
throw new Error(
7574
"type cache failed:" +
76-
JSON.stringify({ code, success, stdout: out, stderr: err })
75+
JSON.stringify({ code, success, stdout: out, stderr: err }),
7776
);
7877
}
7978
}

check.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if (import.meta.main) {
2222

2323
async function parallel_check(
2424
entry_iter: AsyncIterableIterator<WalkEntry>,
25-
limiter: AsyncCurrentLimiter
25+
limiter: AsyncCurrentLimiter,
2626
) {
2727
const files: string[] = [];
2828

@@ -37,21 +37,20 @@ async function parallel_check(
3737
limiter.run(
3838
function (stack: string[]) {
3939
return runDenoCheck(stack);
40-
}.bind(null, stack)
40+
}.bind(null, stack),
4141
)
42-
)
42+
),
4343
);
4444
}
4545
async function start() {
4646
const limiter = new AsyncLimiterClass(navigator.hardwareConcurrency);
4747
const args = parse(Deno.args);
4848
console.log(args);
49-
const skip =
50-
typeof args.skip === "string"
51-
? new RegExp(String(args.skip))
52-
: Array.isArray(args.skip)
53-
? args.skip.map((s: string | RegExp) => new RegExp(s))
54-
: undefined;
49+
const skip = typeof args.skip === "string"
50+
? new RegExp(String(args.skip))
51+
: Array.isArray(args.skip)
52+
? args.skip.map((s: string | RegExp) => new RegExp(s))
53+
: undefined;
5554
const entry_iter = searchFilesNames({ skip });
5655
await parallel_check(entry_iter, limiter);
5756
}
@@ -73,7 +72,7 @@ async function runDenoCheck(stack: string[]) {
7372
if (!success) {
7473
throw new Error(
7574
"type cache failed:" +
76-
JSON.stringify({ code, success, stdout: out, stderr: err })
75+
JSON.stringify({ code, success, stdout: out, stderr: err }),
7776
);
7877
}
7978
}

chunk-array/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type JSONValue = null | boolean | number | string | JSONValue[] | {
2+
[key: string]: JSONValue;
3+
};
4+
export type Obj = Record<string, JSONValue> | Array<JSONValue>;
5+
export default function chunk(arr: Obj[], size: number): Obj[][] {
6+
const res = Array(Math.ceil(arr.length / size)).fill([]);
7+
return res.map((_c, ci) => arr.slice(ci * size, size + ci * size));
8+
}

counter-ii/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export type Counter = {
2+
increment: () => number;
3+
decrement: () => number;
4+
reset: () => number;
5+
};
6+
7+
export default function createCounter(init: number): Counter {
8+
let n = init;
9+
return {
10+
increment: () => ++n,
11+
decrement: () => --n,
12+
reset: () => (n = init),
13+
};
14+
}

create-hello-world-function/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function createHelloWorld(): (...args: any[]) => string {
2+
return function (..._args): string {
3+
return "Hello World";
4+
};
5+
}

flipping-an-image/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default function flipAndInvertImage(image: number[][]): number[][] {
2+
const n = image.length;
3+
for (let i = 0; i < n; i++) {
4+
let left = 0, right = n - 1;
5+
while (left < right) {
6+
if (image[i][left] === image[i][right]) {
7+
image[i][left] ^= 1;
8+
image[i][right] ^= 1;
9+
}
10+
left++;
11+
right--;
12+
}
13+
if (left === right) {
14+
image[i][left] ^= 1;
15+
}
16+
}
17+
return image;
18+
}

retry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function retry<T>(
1515
retryOnError?:
1616
| ((error: any) => Promise<boolean>)
1717
| ((error: any) => boolean);
18-
}
18+
},
1919
) {
2020
const retryOnError = opts?.retryOnError ?? (() => true);
2121
const options: Required<RetryOptions> = {

xmake.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { retry } from "./retry.ts";
66

77
async function* findFilesRecursive(
88
path: string,
9-
name: string
9+
name: string,
1010
): AsyncGenerator<string, void, unknown> {
1111
for await (const entry of Deno.readDir(path)) {
1212
const fullPath = resolve(join(path, entry.name));
@@ -37,7 +37,7 @@ async function RunXmake(
3737
sdk: string,
3838
executable: string,
3939
group: string,
40-
mode: string
40+
mode: string,
4141
) {
4242
await RunXmakeConfig(file, toolchain, sdk, executable, mode);
4343
await retry(RunXmakeBuild.bind(null, file, executable, group), {
@@ -50,11 +50,11 @@ async function RunXmake(
5050
const cwd = path.dirname(file);
5151
const dirtobecreate = path.join(
5252
cwd,
53-
path.dirname(filepathmatched)
53+
path.dirname(filepathmatched),
5454
);
5555

5656
console.log(
57-
"Ensures that the directory exists:" + dirtobecreate
57+
"Ensures that the directory exists:" + dirtobecreate,
5858
);
5959
await ensureDir(dirtobecreate);
6060
return true;
@@ -69,7 +69,7 @@ async function RunXmakeConfig(
6969
toolchain: string,
7070
sdk: string,
7171
executable: string,
72-
mode: string
72+
mode: string,
7373
) {
7474
console.log({ file });
7575
const cwd = path.dirname(file);
@@ -88,10 +88,9 @@ export async function RunCommandShell(others: string[], cwd?: string) {
8888

8989
const cmd = os === "windows" ? "powershell.exe" : "bash";
9090

91-
const args =
92-
os === "windows"
93-
? ["-command", others.join(" \n ")]
94-
: ["-c", others.join(" && ")];
91+
const args = os === "windows"
92+
? ["-command", others.join(" \n ")]
93+
: ["-c", others.join(" && ")];
9594

9695
console.log(JSON.stringify({ cmd, cwd, args }));
9796
const command = new Deno.Command(cmd, { cwd: cwd, args });

0 commit comments

Comments
 (0)