Skip to content

Commit b88c1ea

Browse files
authored
Merge branch 'doocs:main' into main
2 parents d625502 + 15492af commit b88c1ea

File tree

883 files changed

+59355
-41337
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

883 files changed

+59355
-41337
lines changed

images/starcharts.svg

Lines changed: 28143 additions & 27831 deletions
Loading

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.

lcof2/剑指 Offer II 098. 路径的数目/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,30 @@ var uniquePaths = function (m, n) {
247247
};
248248
```
249249

250+
#### Swift
251+
252+
```swift
253+
class Solution {
254+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
255+
var dp = Array(repeating: Array(repeating: 0, count: n), count: m)
256+
dp[0][0] = 1
257+
258+
for i in 0..<m {
259+
for j in 0..<n {
260+
if i > 0 {
261+
dp[i][j] += dp[i - 1][j]
262+
}
263+
if j > 0 {
264+
dp[i][j] += dp[i][j - 1]
265+
}
266+
}
267+
}
268+
269+
return dp[m - 1][n - 1]
270+
}
271+
}
272+
```
273+
250274
<!-- tabs:end -->
251275

252276
<!-- solution:end -->
@@ -362,6 +386,24 @@ var uniquePaths = function (m, n) {
362386
};
363387
```
364388

389+
#### Swift
390+
391+
```swift
392+
class Solution {
393+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
394+
var dp = Array(repeating: Array(repeating: 1, count: n), count: m)
395+
396+
for i in 1..<m {
397+
for j in 1..<n {
398+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
399+
}
400+
}
401+
402+
return dp[m-1][n-1]
403+
}
404+
}
405+
```
406+
365407
<!-- tabs:end -->
366408

367409
<!-- solution:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
3+
var dp = Array(repeating: Array(repeating: 0, count: n), count: m)
4+
dp[0][0] = 1
5+
6+
for i in 0..<m {
7+
for j in 0..<n {
8+
if i > 0 {
9+
dp[i][j] += dp[i - 1][j]
10+
}
11+
if j > 0 {
12+
dp[i][j] += dp[i][j - 1]
13+
}
14+
}
15+
}
16+
17+
return dp[m - 1][n - 1]
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func uniquePaths(_ m: Int, _ n: Int) -> Int {
3+
var dp = Array(repeating: Array(repeating: 1, count: n), count: m)
4+
5+
for i in 1..<m {
6+
for j in 1..<n {
7+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
8+
}
9+
}
10+
11+
return dp[m-1][n-1]
12+
}
13+
}

lcof2/剑指 Offer II 099. 最小路径之和/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,34 @@ public class Solution {
202202
}
203203
```
204204

205+
#### Swift
206+
207+
```swift
208+
class Solution {
209+
func minPathSum(_ grid: [[Int]]) -> Int {
210+
let m = grid.count
211+
let n = grid[0].count
212+
var dp = grid
213+
214+
for i in 1..<m {
215+
dp[i][0] += dp[i-1][0]
216+
}
217+
218+
for j in 1..<n {
219+
dp[0][j] += dp[0][j-1]
220+
}
221+
222+
for i in 1..<m {
223+
for j in 1..<n {
224+
dp[i][j] += min(dp[i-1][j], dp[i][j-1])
225+
}
226+
}
227+
228+
return dp[m-1][n-1]
229+
}
230+
}
231+
```
232+
205233
<!-- tabs:end -->
206234

207235
<!-- solution:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
func minPathSum(_ grid: [[Int]]) -> Int {
3+
let m = grid.count
4+
let n = grid[0].count
5+
var dp = grid
6+
7+
for i in 1..<m {
8+
dp[i][0] += dp[i-1][0]
9+
}
10+
11+
for j in 1..<n {
12+
dp[0][j] += dp[0][j-1]
13+
}
14+
15+
for i in 1..<m {
16+
for j in 1..<n {
17+
dp[i][j] += min(dp[i-1][j], dp[i][j-1])
18+
}
19+
}
20+
21+
return dp[m-1][n-1]
22+
}
23+
}

0 commit comments

Comments
 (0)