Climbing Stairs

来源:互联网 发布:晃然学姐的淘宝店 编辑:程序博客网 时间:2024/06/15 07:34

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?

这是一个简单的Fibonaci问题,只是前三个元素应为0,1,2.

public int climbStairs(int n) {if(n<=2)return n;long one=1,two=2,now=0;for(int i=3;i<=n;i++){now=one+two;one=two;two=now;}return (int) now;}


0 0
原创粉丝点击