Problem:
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?
Analysis:
Recursive solution is slow and could not pass online judge because time limit exceeded. But I don't familiar with dynamic programming right now.
Solution:
1: public class Solution {
2: public int climbStairs(int n) {
3: if(n==1) return 1;
4: if(n==2) return 2;
5: return climbStairs(n-1)+climbStairs(n-2);
6: }
7: }
没有评论:
发表评论