Skip to content

Commit ffdd1bb

Browse files
committed
feat: add solutions to lc problem: No.0935
No.0935.Knight Dialer
1 parent 3456f45 commit ffdd1bb

File tree

6 files changed

+335
-2
lines changed

6 files changed

+335
-2
lines changed

solution/0900-0999/0935.Knight Dialer/README.md

+116-1
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,137 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
**方法一:递推**
67+
6668
<!-- tabs:start -->
6769

6870
### **Python3**
6971

7072
<!-- 这里可写当前语言的特殊实现逻辑 -->
7173

7274
```python
73-
75+
class Solution:
76+
def knightDialer(self, n: int) -> int:
77+
if n == 1:
78+
return 10
79+
f = [1] * 10
80+
for _ in range(n - 1):
81+
t = [0] * 10
82+
t[0] = f[4] + f[6]
83+
t[1] = f[6] + f[8]
84+
t[2] = f[7] + f[9]
85+
t[3] = f[4] + f[8]
86+
t[4] = f[0] + f[3] + f[9]
87+
t[6] = f[0] + f[1] + f[7]
88+
t[7] = f[2] + f[6]
89+
t[8] = f[1] + f[3]
90+
t[9] = f[2] + f[4]
91+
f = t
92+
return sum(t) % (10**9 + 7)
7493
```
7594

7695
### **Java**
7796

7897
<!-- 这里可写当前语言的特殊实现逻辑 -->
7998

8099
```java
100+
class Solution {
101+
private static final int MOD = (int) 1e9 + 7;
102+
103+
public int knightDialer(int n) {
104+
if (n == 1) {
105+
return 10;
106+
}
107+
long[] f = new long[10];
108+
Arrays.fill(f, 1);
109+
while (--n > 0) {
110+
long[] t = new long[10];
111+
t[0] = f[4] + f[6];
112+
t[1] = f[6] + f[8];
113+
t[2] = f[7] + f[9];
114+
t[3] = f[4] + f[8];
115+
t[4] = f[0] + f[3] + f[9];
116+
t[6] = f[0] + f[1] + f[7];
117+
t[7] = f[2] + f[6];
118+
t[8] = f[1] + f[3];
119+
t[9] = f[2] + f[4];
120+
for (int i = 0; i < 10; ++i) {
121+
f[i] = t[i] % MOD;
122+
}
123+
}
124+
long ans = 0;
125+
for (long v : f) {
126+
ans = (ans + v) % MOD;
127+
}
128+
return (int) ans;
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
using ll = long long;
137+
138+
class Solution {
139+
public:
140+
int knightDialer(int n) {
141+
if (n == 1) return 10;
142+
int mod = 1e9 + 7;
143+
vector<ll> f(10, 1ll);
144+
while (--n)
145+
{
146+
vector<ll> t(10);
147+
t[0] = f[4] + f[6];
148+
t[1] = f[6] + f[8];
149+
t[2] = f[7] + f[9];
150+
t[3] = f[4] + f[8];
151+
t[4] = f[0] + f[3] + f[9];
152+
t[6] = f[0] + f[1] + f[7];
153+
t[7] = f[2] + f[6];
154+
t[8] = f[1] + f[3];
155+
t[9] = f[2] + f[4];
156+
for (int i = 0; i < 10; ++i) f[i] = t[i] % mod;
157+
}
158+
ll ans = accumulate(f.begin(), f.end(), 0ll);
159+
return (int) (ans % mod);
160+
}
161+
};
162+
```
81163
164+
### **Go**
165+
166+
```go
167+
func knightDialer(n int) int {
168+
if n == 1 {
169+
return 10
170+
}
171+
f := make([]int, 10)
172+
for i := range f {
173+
f[i] = 1
174+
}
175+
mod := int(1e9) + 7
176+
for i := 1; i < n; i++ {
177+
t := make([]int, 10)
178+
t[0] = f[4] + f[6]
179+
t[1] = f[6] + f[8]
180+
t[2] = f[7] + f[9]
181+
t[3] = f[4] + f[8]
182+
t[4] = f[0] + f[3] + f[9]
183+
t[6] = f[0] + f[1] + f[7]
184+
t[7] = f[2] + f[6]
185+
t[8] = f[1] + f[3]
186+
t[9] = f[2] + f[4]
187+
for j, v := range t {
188+
f[j] = v % mod
189+
}
190+
}
191+
ans := 0
192+
for _, v := range f {
193+
ans = (ans + v) % mod
194+
}
195+
return ans
196+
}
82197
```
83198

84199
### **...**

solution/0900-0999/0935.Knight Dialer/README_EN.md

+114-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,126 @@
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def knightDialer(self, n: int) -> int:
60+
if n == 1:
61+
return 10
62+
f = [1] * 10
63+
for _ in range(n - 1):
64+
t = [0] * 10
65+
t[0] = f[4] + f[6]
66+
t[1] = f[6] + f[8]
67+
t[2] = f[7] + f[9]
68+
t[3] = f[4] + f[8]
69+
t[4] = f[0] + f[3] + f[9]
70+
t[6] = f[0] + f[1] + f[7]
71+
t[7] = f[2] + f[6]
72+
t[8] = f[1] + f[3]
73+
t[9] = f[2] + f[4]
74+
f = t
75+
return sum(t) % (10**9 + 7)
5976
```
6077

6178
### **Java**
6279

6380
```java
81+
class Solution {
82+
private static final int MOD = (int) 1e9 + 7;
83+
84+
public int knightDialer(int n) {
85+
if (n == 1) {
86+
return 10;
87+
}
88+
long[] f = new long[10];
89+
Arrays.fill(f, 1);
90+
while (--n > 0) {
91+
long[] t = new long[10];
92+
t[0] = f[4] + f[6];
93+
t[1] = f[6] + f[8];
94+
t[2] = f[7] + f[9];
95+
t[3] = f[4] + f[8];
96+
t[4] = f[0] + f[3] + f[9];
97+
t[6] = f[0] + f[1] + f[7];
98+
t[7] = f[2] + f[6];
99+
t[8] = f[1] + f[3];
100+
t[9] = f[2] + f[4];
101+
for (int i = 0; i < 10; ++i) {
102+
f[i] = t[i] % MOD;
103+
}
104+
}
105+
long ans = 0;
106+
for (long v : f) {
107+
ans = (ans + v) % MOD;
108+
}
109+
return (int) ans;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
using ll = long long;
118+
119+
class Solution {
120+
public:
121+
int knightDialer(int n) {
122+
if (n == 1) return 10;
123+
int mod = 1e9 + 7;
124+
vector<ll> f(10, 1ll);
125+
while (--n)
126+
{
127+
vector<ll> t(10);
128+
t[0] = f[4] + f[6];
129+
t[1] = f[6] + f[8];
130+
t[2] = f[7] + f[9];
131+
t[3] = f[4] + f[8];
132+
t[4] = f[0] + f[3] + f[9];
133+
t[6] = f[0] + f[1] + f[7];
134+
t[7] = f[2] + f[6];
135+
t[8] = f[1] + f[3];
136+
t[9] = f[2] + f[4];
137+
for (int i = 0; i < 10; ++i) f[i] = t[i] % mod;
138+
}
139+
ll ans = accumulate(f.begin(), f.end(), 0ll);
140+
return (int) (ans % mod);
141+
}
142+
};
143+
```
64144
145+
### **Go**
146+
147+
```go
148+
func knightDialer(n int) int {
149+
if n == 1 {
150+
return 10
151+
}
152+
f := make([]int, 10)
153+
for i := range f {
154+
f[i] = 1
155+
}
156+
mod := int(1e9) + 7
157+
for i := 1; i < n; i++ {
158+
t := make([]int, 10)
159+
t[0] = f[4] + f[6]
160+
t[1] = f[6] + f[8]
161+
t[2] = f[7] + f[9]
162+
t[3] = f[4] + f[8]
163+
t[4] = f[0] + f[3] + f[9]
164+
t[6] = f[0] + f[1] + f[7]
165+
t[7] = f[2] + f[6]
166+
t[8] = f[1] + f[3]
167+
t[9] = f[2] + f[4]
168+
for j, v := range t {
169+
f[j] = v % mod
170+
}
171+
}
172+
ans := 0
173+
for _, v := range f {
174+
ans = (ans + v) % mod
175+
}
176+
return ans
177+
}
65178
```
66179

67180
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using ll = long long;
2+
3+
class Solution {
4+
public:
5+
int knightDialer(int n) {
6+
if (n == 1) return 10;
7+
int mod = 1e9 + 7;
8+
vector<ll> f(10, 1ll);
9+
while (--n)
10+
{
11+
vector<ll> t(10);
12+
t[0] = f[4] + f[6];
13+
t[1] = f[6] + f[8];
14+
t[2] = f[7] + f[9];
15+
t[3] = f[4] + f[8];
16+
t[4] = f[0] + f[3] + f[9];
17+
t[6] = f[0] + f[1] + f[7];
18+
t[7] = f[2] + f[6];
19+
t[8] = f[1] + f[3];
20+
t[9] = f[2] + f[4];
21+
for (int i = 0; i < 10; ++i) f[i] = t[i] % mod;
22+
}
23+
ll ans = accumulate(f.begin(), f.end(), 0ll);
24+
return (int) (ans % mod);
25+
}
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
func knightDialer(n int) int {
2+
if n == 1 {
3+
return 10
4+
}
5+
f := make([]int, 10)
6+
for i := range f {
7+
f[i] = 1
8+
}
9+
mod := int(1e9) + 7
10+
for i := 1; i < n; i++ {
11+
t := make([]int, 10)
12+
t[0] = f[4] + f[6]
13+
t[1] = f[6] + f[8]
14+
t[2] = f[7] + f[9]
15+
t[3] = f[4] + f[8]
16+
t[4] = f[0] + f[3] + f[9]
17+
t[6] = f[0] + f[1] + f[7]
18+
t[7] = f[2] + f[6]
19+
t[8] = f[1] + f[3]
20+
t[9] = f[2] + f[4]
21+
for j, v := range t {
22+
f[j] = v % mod
23+
}
24+
}
25+
ans := 0
26+
for _, v := range f {
27+
ans = (ans + v) % mod
28+
}
29+
return ans
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
private static final int MOD = (int) 1e9 + 7;
3+
4+
public int knightDialer(int n) {
5+
if (n == 1) {
6+
return 10;
7+
}
8+
long[] f = new long[10];
9+
Arrays.fill(f, 1);
10+
while (--n > 0) {
11+
long[] t = new long[10];
12+
t[0] = f[4] + f[6];
13+
t[1] = f[6] + f[8];
14+
t[2] = f[7] + f[9];
15+
t[3] = f[4] + f[8];
16+
t[4] = f[0] + f[3] + f[9];
17+
t[6] = f[0] + f[1] + f[7];
18+
t[7] = f[2] + f[6];
19+
t[8] = f[1] + f[3];
20+
t[9] = f[2] + f[4];
21+
for (int i = 0; i < 10; ++i) {
22+
f[i] = t[i] % MOD;
23+
}
24+
}
25+
long ans = 0;
26+
for (long v : f) {
27+
ans = (ans + v) % MOD;
28+
}
29+
return (int) ans;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def knightDialer(self, n: int) -> int:
3+
if n == 1:
4+
return 10
5+
f = [1] * 10
6+
for _ in range(n - 1):
7+
t = [0] * 10
8+
t[0] = f[4] + f[6]
9+
t[1] = f[6] + f[8]
10+
t[2] = f[7] + f[9]
11+
t[3] = f[4] + f[8]
12+
t[4] = f[0] + f[3] + f[9]
13+
t[6] = f[0] + f[1] + f[7]
14+
t[7] = f[2] + f[6]
15+
t[8] = f[1] + f[3]
16+
t[9] = f[2] + f[4]
17+
f = t
18+
return sum(t) % (10**9 + 7)

0 commit comments

Comments
 (0)