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 e498df6

Browse files
更新 0746.使用最小花费爬楼梯.md Java代码
添加第一步支付费用版本的Java代码 删除原版本中冗余的代码,已测试代码 添加CPP代码的语法高亮
1 parent 0edf01e commit e498df6

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

‎problems/0746.使用最小花费爬楼梯.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
所以初始化代码为:
6868

69-
```
69+
```CPP
7070
vector<int> dp(cost.size());
7171
dp[0] = cost[0];
7272
dp[1] = cost[1];
@@ -201,15 +201,32 @@ public:
201201

202202

203203
### Java
204+
204205
```Java
206+
// 方式一:第一步支付费用
205207
class Solution {
206208
public int minCostClimbingStairs(int[] cost) {
207-
if (cost == null || cost.length == 0) {
208-
return 0;
209-
}
210-
if (cost.length == 1) {
211-
return cost[0];
209+
int len = cost.length;
210+
int[] dp = new int[len + 1];
211+
212+
// 从下标为 0 或下标为 1 的台阶开始,因此支付费用为0
213+
dp[0] = 0;
214+
dp[1] = 0;
215+
216+
// 计算到达每一层台阶的最小费用
217+
for (int i = 2; i <= len; i++) {
218+
dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
212219
}
220+
221+
return dp[len];
222+
}
223+
}
224+
```
225+
226+
```Java
227+
// 方式二:第一步不支付费用
228+
class Solution {
229+
public int minCostClimbingStairs(int[] cost) {
213230
int[] dp = new int[cost.length];
214231
dp[0] = cost[0];
215232
dp[1] = cost[1];

0 commit comments

Comments
(0)

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