Topic:
You is climbing a stair case. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In what many distinct ways can you climb to the top?
Note:given N would be a positive integer.
test instructions and analysis: There is an n-step ladder, can only climb one or two steps at a time, to find out how many ways to climb the ladder from the total number of methods. The topic of dynamic planning, because there are only two methods to step I (either to climb two steps from i-2 or a step from i-1), so go to step I of D (i) =d (i-1) +d (i-1), that is, to the i-1 step of the walk Plus to the i-2 of the method. So we can find out the solution by traversing to nth step.
Code:
public class Solution {public int climbstairs (int n) {int[] a=new int[n];//used to record the number of steps to take in each step a[0]=1;if (n==1) return 1; a[1]=2;for (int i=2;i<n;i++) {a[i]=a[i-1]+a[i-2];} return a[n-1];}}
[Leetcode] 70. Climbing Stairs Java