[leetcode] #70 Climb stairs
這題很簡單, 題目要求給n個階梯, 若每次僅走一步或兩步, 則走到第n階的路徑有多少種。
(思路)
1. 第 i-th 階 step[i] 儘可能從 step[i-1] 或 step[i-2] 出發.
2. 要留意 1st 跟 2rd 階的算法
(思路)
1. 第 i-th 階 step[i] 儘可能從 step[i-1] 或 step[i-2] 出發.
2. 要留意 1st 跟 2rd 階的算法
1: public class Solution {
2: public int climbStairs(int n) {
3: if (n<=0)
4: return 0;
5: int[] steps = new int[n+1];
6: steps[0] = 1;
7: steps[1] = 1;
8: for (int i=2;i<=n;i++) {
9: steps[i] = steps[i-1] + steps[i-2];
10: }
11: return steps[n];
12: }
13: }
Comments
Post a Comment