Skip to content

Commit 59e45fa

Browse files
committed
feat: add python and java solution to lcof problem
1 parent 2d4c5aa commit 59e45fa

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

lcof/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@
2626
│   ├── README.md
2727
│   ├── Solution.java
2828
│   └── Solution.py
29-
└── 面试题09. 用两个栈实现队列
29+
├── 面试题09. 用两个栈实现队列
30+
│   ├── README.md
31+
│   ├── Solution.java
32+
│   └── Solution.py
33+
├── 面试题10- I. 斐波那契数列
34+
│   ├── README.md
35+
│   ├── Solution.java
36+
│   └── Solution.py
37+
└── 面试题10- II. 青蛙跳台阶问题
3038
├── README.md
3139
├── Solution.java
3240
└── Solution.py
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# [面试题10- II. 青蛙跳台阶问题](https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof/)
2+
3+
## 题目描述
4+
一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级台阶。求该青蛙跳上一个 `n` 级的台阶总共有多少种跳法。
5+
6+
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
7+
8+
**示例 1:**
9+
10+
```
11+
输入:n = 2
12+
输出:2
13+
```
14+
15+
**示例 2:**
16+
17+
```
18+
输入:n = 7
19+
输出:21
20+
```
21+
22+
**提示:**
23+
24+
- `0 <= n <= 100`
25+
26+
27+
## 解法
28+
### Python3
29+
```python
30+
class Solution:
31+
def numWays(self, n: int) -> int:
32+
a, b = 0, 1
33+
for _ in range(n):
34+
s = a + b
35+
a, b = b, s
36+
return b % 1000000007
37+
```
38+
39+
### Java
40+
```java
41+
class Solution {
42+
public int numWays(int n) {
43+
int a = 0, b = 1;
44+
for (int i = 0; i < n; ++i) {
45+
int s = (a + b) % 1000000007;
46+
a = b;
47+
b = s;
48+
}
49+
return b;
50+
}
51+
}
52+
```
53+
54+
### ...
55+
```
56+
57+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int numWays(int n) {
3+
int a = 0, b = 1;
4+
for (int i = 0; i < n; ++i) {
5+
int s = (a + b) % 1000000007;
6+
a = b;
7+
b = s;
8+
}
9+
return b;
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def numWays(self, n: int) -> int:
3+
a, b = 0, 1
4+
for _ in range(n):
5+
s = a + b
6+
a, b = b, s
7+
return b % 1000000007

0 commit comments

Comments
 (0)