[leetcode]Climbing Stairs


新博文地址:[leetcode]Climbing Stairs

http://oj.leetcode.com/problems/climbing-stairs/

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?

 是不是很熟悉?跟青蛙上台阶一样一样滴。。。菲波那切数列,不罗嗦

两种方法:

1. 递归(大量冗余计算,可能超时)

2. 循环

   public int climbStairs(int n) {
if (n == 0 || n == 1) {
return 1;
}
int[] array = new int[n + 1];
array[1] = 1;
array[2] = 2;

for (int i = 3; i <= n; i++) {
array[i] = array[i-1] + array[i-2];
}
return array[n];
}

 

 

本站声明
本文转载自:http://huntfor.iteye.com/blog/2051393     作者:huntfor     发布日期:2014-04-21 16:55:00     本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。


 
© 2014-2016 ITdaan.com 粤ICP备14056181号