问题描述:
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?
原问题链接:https://leetcode.com/problems/climbing-stairs/
问题分析
这个问题在之前的文章里有讨论过。它本质上就是一个fibonacci数列的问题。我们可以用一个简单的循环递推去求这个结果。关于它的解法在这里就不详述了。详细的代码实现如下:
public class Solution { public int climbStairs(int n) { if(n <= 1) return 1; int a = 1, b = 1, c = 0; for(int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return c; } }