Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f82eaaa

Browse files
feat: update solutions to lc problems: No.508,1137,1220 (doocs#3571)
1 parent 0226650 commit f82eaaa

File tree

29 files changed

+957
-720
lines changed

29 files changed

+957
-720
lines changed

‎lcci/08.01.Three Steps Problem/README.md‎

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.01.Three%20Steps%2
1919
<p> <strong>示例1:</strong></p>
2020

2121
<pre>
22-
<strong> 输入</strong>:n = 3
22+
<strong> 输入</strong>:n = 3
2323
<strong> 输出</strong>:4
2424
<strong> 说明</strong>: 有四种走法
2525
</pre>
@@ -226,37 +226,6 @@ $$
226226

227227
#### Python3
228228

229-
```python
230-
class Solution:
231-
def waysToStep(self, n: int) -> int:
232-
mod = 10**9 + 7
233-
234-
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
235-
m, n = len(a), len(b[0])
236-
c = [[0] * n for _ in range(m)]
237-
for i in range(m):
238-
for j in range(n):
239-
for k in range(len(a[0])):
240-
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
241-
return c
242-
243-
def pow(a: List[List[int]], n: int) -> List[List[int]]:
244-
res = [[4, 2, 1]]
245-
while n:
246-
if n & 1:
247-
res = mul(res, a)
248-
n >>= 1
249-
a = mul(a, a)
250-
return res
251-
252-
if n < 4:
253-
return 2 ** (n - 1)
254-
a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
255-
return sum(pow(a, n - 4)[0]) % mod
256-
```
257-
258-
#### Python3
259-
260229
```python
261230
import numpy as np
262231

@@ -266,8 +235,8 @@ class Solution:
266235
if n < 4:
267236
return 2 ** (n - 1)
268237
mod = 10**9 + 7
269-
factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
270-
res = np.mat([(4, 2, 1)], np.dtype("O"))
238+
factor = np.asmatrix([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
239+
res = np.asmatrix([(4, 2, 1)], np.dtype("O"))
271240
n -= 4
272241
while n:
273242
if n & 1:

‎lcci/08.01.Three Steps Problem/README_EN.md‎

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/08.01.Three%20Steps%2
2020

2121
<pre>
2222

23-
<strong> Input</strong>: n = 3
23+
<strong> Input</strong>: n = 3
2424

2525
<strong> Output</strong>: 4
2626

@@ -229,37 +229,6 @@ The time complexity is $O(\log n),ドル and the space complexity is $O(1)$.
229229

230230
#### Python3
231231

232-
```python
233-
class Solution:
234-
def waysToStep(self, n: int) -> int:
235-
mod = 10**9 + 7
236-
237-
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
238-
m, n = len(a), len(b[0])
239-
c = [[0] * n for _ in range(m)]
240-
for i in range(m):
241-
for j in range(n):
242-
for k in range(len(a[0])):
243-
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
244-
return c
245-
246-
def pow(a: List[List[int]], n: int) -> List[List[int]]:
247-
res = [[4, 2, 1]]
248-
while n:
249-
if n & 1:
250-
res = mul(res, a)
251-
n >>= 1
252-
a = mul(a, a)
253-
return res
254-
255-
if n < 4:
256-
return 2 ** (n - 1)
257-
a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
258-
return sum(pow(a, n - 4)[0]) % mod
259-
```
260-
261-
#### Python3
262-
263232
```python
264233
import numpy as np
265234

@@ -269,8 +238,8 @@ class Solution:
269238
if n < 4:
270239
return 2 ** (n - 1)
271240
mod = 10**9 + 7
272-
factor = np.mat([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
273-
res = np.mat([(4, 2, 1)], np.dtype("O"))
241+
factor = np.asmatrix([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
242+
res = np.asmatrix([(4, 2, 1)], np.dtype("O"))
274243
n -= 4
275244
while n:
276245
if n & 1:
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
class Solution:
2-
def waysToStep(self, n: int) -> int:
3-
mod = 10**9 + 7
4-
5-
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
6-
m, n = len(a), len(b[0])
7-
c = [[0] * n for _ in range(m)]
8-
for i in range(m):
9-
for j in range(n):
10-
for k in range(len(a[0])):
11-
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod
12-
return c
1+
import numpy as np
132

14-
def pow(a: List[List[int]], n: int) -> List[List[int]]:
15-
res = [[4, 2, 1]]
16-
while n:
17-
if n & 1:
18-
res = mul(res, a)
19-
n >>= 1
20-
a = mul(a, a)
21-
return res
223

4+
class Solution:
5+
def waysToStep(self, n: int) -> int:
236
if n < 4:
247
return 2 ** (n - 1)
25-
a = [[1, 1, 0], [1, 0, 1], [1, 0, 0]]
26-
return sum(pow(a, n - 4)[0]) % mod
8+
mod = 10**9 + 7
9+
factor = np.asmatrix([(1, 1, 0), (1, 0, 1), (1, 0, 0)], np.dtype("O"))
10+
res = np.asmatrix([(4, 2, 1)], np.dtype("O"))
11+
n -= 4
12+
while n:
13+
if n & 1:
14+
res = res * factor % mod
15+
factor = factor * factor % mod
16+
n >>= 1
17+
return res.sum() % mod

‎lcci/08.01.Three Steps Problem/Solution3.py‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎solution/0000-0099/0070.Climbing Stairs/README.md‎

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -252,28 +252,20 @@ $$
252252
#### Python3
253253

254254
```python
255+
import numpy as np
256+
257+
255258
class Solution:
256259
def climbStairs(self, n: int) -> int:
257-
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
258-
m, n = len(a), len(b[0])
259-
c = [[0] * n for _ in range(m)]
260-
for i in range(m):
261-
for j in range(n):
262-
for k in range(len(a[0])):
263-
c[i][j] = c[i][j] + a[i][k] * b[k][j]
264-
return c
265-
266-
def pow(a: List[List[int]], n: int) -> List[List[int]]:
267-
res = [[1, 1]]
268-
while n:
269-
if n & 1:
270-
res = mul(res, a)
271-
n >>= 1
272-
a = mul(a, a)
273-
return res
274-
275-
a = [[1, 1], [1, 0]]
276-
return pow(a, n - 1)[0][0]
260+
res = np.asmatrix([(1, 1)], np.dtype("O"))
261+
factor = np.asmatrix([(1, 1), (1, 0)], np.dtype("O"))
262+
n -= 1
263+
while n:
264+
if n & 1:
265+
res *= factor
266+
factor *= factor
267+
n >>= 1
268+
return res[0, 0]
277269
```
278270

279271
#### Java
@@ -478,33 +470,4 @@ function pow(a, n) {
478470

479471
<!-- solution:end -->
480472

481-
<!-- solution:start -->
482-
483-
### 方法三
484-
485-
<!-- tabs:start -->
486-
487-
#### Python3
488-
489-
```python
490-
import numpy as np
491-
492-
493-
class Solution:
494-
def climbStairs(self, n: int) -> int:
495-
res = np.mat([(1, 1)], np.dtype("O"))
496-
factor = np.mat([(1, 1), (1, 0)], np.dtype("O"))
497-
n -= 1
498-
while n:
499-
if n & 1:
500-
res *= factor
501-
factor *= factor
502-
n >>= 1
503-
return res[0, 0]
504-
```
505-
506-
<!-- tabs:end -->
507-
508-
<!-- solution:end -->
509-
510473
<!-- problem:end -->

‎solution/0000-0099/0070.Climbing Stairs/README_EN.md‎

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -251,28 +251,20 @@ The time complexity is $O(\log n),ドル and the space complexity is $O(1)$.
251251
#### Python3
252252

253253
```python
254+
import numpy as np
255+
256+
254257
class Solution:
255258
def climbStairs(self, n: int) -> int:
256-
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
257-
m, n = len(a), len(b[0])
258-
c = [[0] * n for _ in range(m)]
259-
for i in range(m):
260-
for j in range(n):
261-
for k in range(len(a[0])):
262-
c[i][j] = c[i][j] + a[i][k] * b[k][j]
263-
return c
264-
265-
def pow(a: List[List[int]], n: int) -> List[List[int]]:
266-
res = [[1, 1]]
267-
while n:
268-
if n & 1:
269-
res = mul(res, a)
270-
n >>= 1
271-
a = mul(a, a)
272-
return res
273-
274-
a = [[1, 1], [1, 0]]
275-
return pow(a, n - 1)[0][0]
259+
res = np.asmatrix([(1, 1)], np.dtype("O"))
260+
factor = np.asmatrix([(1, 1), (1, 0)], np.dtype("O"))
261+
n -= 1
262+
while n:
263+
if n & 1:
264+
res *= factor
265+
factor *= factor
266+
n >>= 1
267+
return res[0, 0]
276268
```
277269

278270
#### Java
@@ -477,33 +469,4 @@ function pow(a, n) {
477469

478470
<!-- solution:end -->
479471

480-
<!-- solution:start -->
481-
482-
### Solution 3
483-
484-
<!-- tabs:start -->
485-
486-
#### Python3
487-
488-
```python
489-
import numpy as np
490-
491-
492-
class Solution:
493-
def climbStairs(self, n: int) -> int:
494-
res = np.mat([(1, 1)], np.dtype("O"))
495-
factor = np.mat([(1, 1), (1, 0)], np.dtype("O"))
496-
n -= 1
497-
while n:
498-
if n & 1:
499-
res *= factor
500-
factor *= factor
501-
n >>= 1
502-
return res[0, 0]
503-
```
504-
505-
<!-- tabs:end -->
506-
507-
<!-- solution:end -->
508-
509472
<!-- problem:end -->
Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
class Solution:
2-
def climbStairs(self, n: int) -> int:
3-
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
4-
m, n = len(a), len(b[0])
5-
c = [[0] * n for _ in range(m)]
6-
for i in range(m):
7-
for j in range(n):
8-
for k in range(len(a[0])):
9-
c[i][j] = c[i][j] + a[i][k] * b[k][j]
10-
return c
1+
import numpy as np
112

12-
def pow(a: List[List[int]], n: int) -> List[List[int]]:
13-
res = [[1, 1]]
14-
while n:
15-
if n & 1:
16-
res = mul(res, a)
17-
n >>= 1
18-
a = mul(a, a)
19-
return res
203

21-
a = [[1, 1], [1, 0]]
22-
return pow(a, n - 1)[0][0]
4+
class Solution:
5+
def climbStairs(self, n: int) -> int:
6+
res = np.asmatrix([(1, 1)], np.dtype("O"))
7+
factor = np.asmatrix([(1, 1), (1, 0)], np.dtype("O"))
8+
n -= 1
9+
while n:
10+
if n & 1:
11+
res *= factor
12+
factor *= factor
13+
n >>= 1
14+
return res[0, 0]

‎solution/0000-0099/0070.Climbing Stairs/Solution3.py‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /