You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
描述
n 步的楼梯,每次只能爬 1 步或者 2 步,问有多少种爬的方式
分析
假设爬 k 步有 s[k] 种方法,爬 k-1 步有 s[k-1] 种方法,爬 k-2 步有 s[k-2] 种方法。因为每次只能爬 1 步或者 2 步,所以很明显爬 k 步相当于爬 k-1 步后再多爬 1 步,或者爬 k-2 步后再多爬 2 步,即状态转移方程: s[k] = s[k-1] + s[k-2]
代码
1 | public class Solution { |