LeetCode Climbing Stairs
2015-06-29 21:56
357 查看
Description:
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?
Solution:
dp[i] = dp[i-1] + dp[i-2],其中dp[i]表示走到了第i个用了几种不同的方法,可以理解为加法组合。
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?
Solution:
dp[i] = dp[i-1] + dp[i-2],其中dp[i]表示走到了第i个用了几种不同的方法,可以理解为加法组合。
public class Solution { public int climbStairs(int n) { if (n == 1) return 1; int dp[] = new int ; dp[0] = 1; dp[1] = 2; for (int i = 2; i < n; i++) dp[i] = dp[i - 1] + dp[i - 2]; return dp[n - 1]; } }
相关文章推荐
- BZOJ4155 : [Ipsc2015]Humble Captains
- CheeseZH: Stanford University: Machine Learning Ex4:Training Neural Network(Backpropagation Algorithm)
- How to run oaisim with virtual MME on same machine
- AS 学习笔记之Error:Execution failed for task ':dynamicgrid:packageReleaseResources'.
- vs2010:无法解析的外部符号 _main
- hdu 3394 railway 点连通分量判环
- Maven 工程报错Failure to transfer org.codehaus.plexus:plexus-io:pom:1.0.1 from http://repo.maven.apache.
- android.intent.action.MAIN 与 android.intent.category.LAUNCHER
- IBM X3650 服务器使用SERVERAID 8K-1做RAID1更换硬盘
- IBM System x 服务器自带RAID1的故障恢复 转
- UML期末复习题——2.4:Domain Model
- 警告: Unsupported configuration plain style unsupported in a navigation item
- URAL 1019 Line Painting
- 出现( linker command failed with exit code 1)错误总结
- Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using
- hdu 3394 railway (BCC缩点)
- 解决Determining ip information for eth0... failed
- RAID5原理简介
- 采用aidl绑定远程服务
- Just a complaint about the college examinations