Skip to content

feat: add TS solution to lc problem: No.0752 #2995

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 4 commits into from
Jun 2, 2024
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
33 changes: 33 additions & 0 deletions solution/0700-0799/0752.Open the Lock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,39 @@ func openLock(deadends []string, target string) int {
}
```

#### TypeScript

```ts
function openLock(deadends: string[], target: string): number {
const vis = Array<boolean>(10_000);
const q = [[0, 0, 0, 0, 0]];
const t = +target;
deadends.forEach(s => (vis[+s] = true));

for (const [a, b, c, d, ans] of q) {
const key = a * 1000 + b * 100 + c * 10 + d;
if (vis[key]) {
continue;
}

vis[key] = true;
if (key === t) {
return ans;
}

for (let i = 0; i < 4; i++) {
const next = [a, b, c, d, ans + 1];
const prev = [a, b, c, d, ans + 1];
next[i] = (next[i] + 11) % 10;
prev[i] = (prev[i] + 9) % 10;
q.push(prev, next);
}
}

return -1;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
33 changes: 33 additions & 0 deletions solution/0700-0799/0752.Open the Lock/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,39 @@ func openLock(deadends []string, target string) int {
}
```

#### TypeScript

```ts
function openLock(deadends: string[], target: string): number {
const vis = Array<boolean>(10_000);
const q = [[0, 0, 0, 0, 0]];
const t = +target;
deadends.forEach(s => (vis[+s] = true));

for (const [a, b, c, d, ans] of q) {
const key = a * 1000 + b * 100 + c * 10 + d;
if (vis[key]) {
continue;
}

vis[key] = true;
if (key === t) {
return ans;
}

for (let i = 0; i < 4; i++) {
const next = [a, b, c, d, ans + 1];
const prev = [a, b, c, d, ans + 1];
next[i] = (next[i] + 11) % 10;
prev[i] = (prev[i] + 9) % 10;
q.push(prev, next);
}
}

return -1;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
28 changes: 28 additions & 0 deletions solution/0700-0799/0752.Open the Lock/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function openLock(deadends: string[], target: string): number {
const vis = Array<boolean>(10_000);
const q = [[0, 0, 0, 0, 0]];
const t = +target;
deadends.forEach(s => (vis[+s] = true));

for (const [a, b, c, d, ans] of q) {
const key = a * 1000 + b * 100 + c * 10 + d;
if (vis[key]) {
continue;
}

vis[key] = true;
if (key === t) {
return ans;
}

for (let i = 0; i < 4; i++) {
const next = [a, b, c, d, ans + 1];
const prev = [a, b, c, d, ans + 1];
next[i] = (next[i] + 11) % 10;
prev[i] = (prev[i] + 9) % 10;
q.push(prev, next);
}
}

return -1;
}
Loading