70. Climbing Stairs

Total Accepted: 93127
Total Submissions: 258282
Difficulty: Easy

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?

Java:

1
2
3
4
5
6
7
8
9
public class Solution {
    public int climbStairs(int n) {
        int[] ans=new int[n+1];
        ans[0]=ans[1]=1;
        for(int i=2;i<=n;++i)
            ans[i]=ans[i-1]+ans[i-2];
        return ans[n];
    }
}

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。