lintcode-easy-Climbing Stairs
2016-02-21 18:07
483 查看
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?
Example
Given an example n=3 , 1+1+1=2+1=1+2=3
return 3
动态规划里思路比较容易理清的一题,要走到第n个台阶有两种选择,要么从第n-1阶走一个台阶上来,要么从第n-2阶走两个台阶上来。
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example
Given an example n=3 , 1+1+1=2+1=1+2=3
return 3
动态规划里思路比较容易理清的一题,要走到第n个台阶有两种选择,要么从第n-1阶走一个台阶上来,要么从第n-2阶走两个台阶上来。
public class Solution { /** * @param n: An integer * @return: An integer */ public int climbStairs(int n) { // write your code here if(n == 0) return 1; if(n == 1) return 1; if(n == 2) return 2; int[] result = new int[n + 1]; result[0] = 1; result[1] = 1; result[2] = 2; for(int i = 3; i <= n; i++) result[i] = result[i - 1] + result[i - 2]; return result ; } }
相关文章推荐
- 可以填空的TextView
- Comparison of video container formats
- Aizu2130-Billion Million Thousand-dp
- raid以及mdadm命令之二
- http://blog.csdn.net/antjumper/article/details/39479187
- Machine Learning - Regularized Logistic Regression
- 2016-02-21-阅读笔记:大脑计划+3rd open GPU+ Delip质疑google Swivel中loss-function + training trick BN in DL
- Unable to simultaneously satisfy constraints.这一问题
- Main方法格式讲解
- main 函数
- reverse mapping checking getaddrinfo for bogon failed – POSSIBLE BREAK-IN ATTEMPT! 错误
- scala中的trait
- vector::clear()和vecotr::pop_back()对内存处理的不同 http://blog.csdn.net/vieri_ch/article/details/1191390
- 关于破解AI的补丁和方法
- hdu 1151 Air Raid(最小路径覆盖)
- CodeForces 630 A. Again Twenty Five!(水~)
- git@xxxx.git: Auth fail错误
- poj-3007 Organize Your Train part II
- 关于linker command failed with exit code 1 (use -v to see invocation)错误
- 最近学习