标签:return air etc view bsp [] cas can positive
leetcode 70. 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?
Note: Given n will be a positive integer.
本题考虑动态规划,每一阶台阶只能从它的前一个台阶来或者从前两个台阶来,那么只需知道前一个台阶和前两个台阶能有几种走法就ok了,
第一个台阶是1中,第二个台阶是两种,注意只有一个台阶的特殊情况
public class Solution { public int climbStairs(int n) { int [] path=new int [n]; if(n==1){ return 1; } path[0]=1; path[1]=2; for (int i=2;i<n;i++){ if (i-2>=0){ path[i]+=path[i-2]; } if (i-1>=0){ path[i]+=path[i-1]; } } return path[n-1]; } }
标签:return air etc view bsp [] cas can positive
原文地址:http://www.cnblogs.com/sure0328/p/7111256.html