-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
feat: add ts solution to lc problem: No.935 #2025
Changes from 3 commits
5c95c8a
ba9eaca
bee0008
c397d0f
12ad019
30eb1ed
de71d49
eb6c52b
8a55e2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,6 +224,45 @@ public class Solution { | |
} | ||
``` | ||
|
||
### **TypeScript** | ||
|
||
```ts | ||
function knightDialer(n: number): number { | ||
const MOD: number = 1e9 + 7; | ||
|
||
if (n === 1) { | ||
return 10; | ||
} | ||
|
||
let f: number[] = new Array(10).fill(1); | ||
|
||
while (--n > 0) { | ||
let t: number[] = new Array(10).fill(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
|
||
t[0] = f[4] + f[6]; | ||
t[1] = f[6] + f[8]; | ||
t[2] = f[7] + f[9]; | ||
t[3] = f[4] + f[8]; | ||
t[4] = f[0] + f[3] + f[9]; | ||
t[6] = f[0] + f[1] + f[7]; | ||
t[7] = f[2] + f[6]; | ||
t[8] = f[1] + f[3]; | ||
t[9] = f[2] + f[4]; | ||
|
||
for (let i = 0; i < 10; ++i) { | ||
f[i] = t[i] % MOD; | ||
} | ||
} | ||
|
||
let ans: number = 0; | ||
for (let v of f) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
ans = (ans + v) % MOD; | ||
} | ||
|
||
return ans; | ||
} | ||
``` | ||
|
||
### **...** | ||
|
||
``` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function knightDialer(n: number): number { | ||
const MOD: number = 1e9 + 7; | ||
|
||
if (n === 1) { | ||
return 10; | ||
} | ||
|
||
let f: number[] = new Array(10).fill(1); | ||
|
||
while (--n > 0) { | ||
let t: number[] = new Array(10).fill(0); | ||
|
||
t[0] = f[4] + f[6]; | ||
t[1] = f[6] + f[8]; | ||
t[2] = f[7] + f[9]; | ||
t[3] = f[4] + f[8]; | ||
t[4] = f[0] + f[3] + f[9]; | ||
t[6] = f[0] + f[1] + f[7]; | ||
t[7] = f[2] + f[6]; | ||
t[8] = f[1] + f[3]; | ||
t[9] = f[2] + f[4]; | ||
|
||
for (let i = 0; i < 10; ++i) { | ||
f[i] = t[i] % MOD; | ||
} | ||
} | ||
|
||
let ans: number = 0; | ||
for (let v of f) { | ||
ans = (ans + v) % MOD; | ||
} | ||
|
||
return ans; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const f: number[] = Array(10).fill(1);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated