LeetCode--No.70--Climbing Stairs

来源:互联网 发布:阿里巴巴能走淘宝联盟 编辑:程序博客网 时间:2024/04/29 08:42

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?

Subscribe to see which companies asked this question

方法一:递归很简单

public class Solution {    public int climbStairs(int n) {        if(n==1)            return 1;        else if(n == 2)            return 2;        else            return climbStairs(n-1) + climbStairs(n-2);    }}
方法二:非递归

public class Solution {    public int climbStairs(int n) {        int a = 0;        int b = 1;        int count = 0;        for(int i = 0; i < n; i++){            count = a+b;            a = b;            b = count;        }        return count;    }}



0 0
原创粉丝点击