Skip to content

Commit f5dd246

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 08.01. 三步问题
1 parent dbd9984 commit f5dd246

File tree

4 files changed

+126
-49
lines changed

4 files changed

+126
-49
lines changed

lcci/08.01.Three Steps Problem/README.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,41 @@
2828

2929
## 解法
3030
<!-- 这里可写通用的实现逻辑 -->
31-
31+
递推法。`f(n)=f(n-1)+f(n-2)+f(n-3)`
3232

3333
### Python3
3434
<!-- 这里可写当前语言的特殊实现逻辑 -->
3535

3636
```python
37-
37+
class Solution:
38+
def waysToStep(self, n: int) -> int:
39+
if n < 3:
40+
return n
41+
a, b, c = 1, 2, 4
42+
for i in range(4, n + 1):
43+
a, b, c = b, c, (a + b + c) % 1000000007
44+
return c
3845
```
3946

4047
### Java
4148
<!-- 这里可写当前语言的特殊实现逻辑 -->
4249

4350
```java
44-
51+
class Solution {
52+
public int waysToStep(int n) {
53+
if (n < 3) {
54+
return n;
55+
}
56+
int a = 1, b = 2, c = 4;
57+
for (int i = 4; i <= n; ++i) {
58+
int t = a;
59+
a = b;
60+
b = c;
61+
c = ((a + b) % 1000000007 + t) % 1000000007;
62+
}
63+
return c;
64+
}
65+
}
4566
```
4667

4768
### ...
+79-46
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,79 @@
1-
# [08.01. Three Steps Problem](https://leetcode-cn.com/problems/three-steps-problem-lcci)
2-
3-
## Description
4-
<p>A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.&nbsp;The result may be large, so return it modulo 1000000007.</p>
5-
6-
<p><strong>Example1:</strong></p>
7-
8-
<pre>
9-
<strong> Input</strong>: n = 3
10-
<strong> Output</strong>: 4
11-
</pre>
12-
13-
<p><strong>Example2:</strong></p>
14-
15-
<pre>
16-
<strong> Input</strong>: n = 5
17-
<strong> Output</strong>: 13
18-
</pre>
19-
20-
<p><strong>Note:</strong></p>
21-
22-
<ol>
23-
<li><code>1 &lt;= n &lt;= 1000000</code></li>
24-
</ol>
25-
26-
27-
28-
## Solutions
29-
30-
31-
### Python3
32-
33-
```python
34-
35-
```
36-
37-
### Java
38-
39-
```java
40-
41-
```
42-
43-
### ...
44-
```
45-
46-
```
1+
# [08.01. Three Steps Problem](https://leetcode-cn.com/problems/three-steps-problem-lcci)
2+
3+
## Description
4+
<p>A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.&nbsp;The result may be large, so return it modulo 1000000007.</p>
5+
6+
7+
8+
<p><strong>Example1:</strong></p>
9+
10+
11+
12+
<pre>
13+
14+
<strong> Input</strong>: n = 3
15+
16+
<strong> Output</strong>: 4
17+
18+
</pre>
19+
20+
21+
22+
<p><strong>Example2:</strong></p>
23+
24+
25+
26+
<pre>
27+
28+
<strong> Input</strong>: n = 5
29+
30+
<strong> Output</strong>: 13
31+
32+
</pre>
33+
34+
35+
36+
<p><strong>Note:</strong></p>
37+
38+
1. `1 <= n <= 1000000`
39+
40+
## Solutions
41+
42+
43+
### Python3
44+
45+
```python
46+
class Solution:
47+
def waysToStep(self, n: int) -> int:
48+
if n < 3:
49+
return n
50+
a, b, c = 1, 2, 4
51+
for i in range(4, n + 1):
52+
a, b, c = b, c, (a + b + c) % 1000000007
53+
return c
54+
```
55+
56+
### Java
57+
58+
```java
59+
class Solution {
60+
public int waysToStep(int n) {
61+
if (n < 3) {
62+
return n;
63+
}
64+
int a = 1, b = 2, c = 4;
65+
for (int i = 4; i <= n; ++i) {
66+
int t = a;
67+
a = b;
68+
b = c;
69+
c = ((a + b) % 1000000007 + t) % 1000000007;
70+
}
71+
return c;
72+
}
73+
}
74+
```
75+
76+
### ...
77+
```
78+
79+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int waysToStep(int n) {
3+
if (n < 3) {
4+
return n;
5+
}
6+
int a = 1, b = 2, c = 4;
7+
for (int i = 4; i <= n; ++i) {
8+
int t = a;
9+
a = b;
10+
b = c;
11+
c = ((a + b) % 1000000007 + t) % 1000000007;
12+
}
13+
return c;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def waysToStep(self, n: int) -> int:
3+
if n < 3:
4+
return n
5+
a, b, c = 1, 2, 4
6+
for i in range(4, n + 1):
7+
a, b, c = b, c, (a + b + c) % 1000000007
8+
return c

0 commit comments

Comments
 (0)