【Leetcode】Climbing Stairs

题目链接:https://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?

思路:

上一步怎么走的跟下一步都的没关系。用data数组表示到每一步为止,有多少种方法。

算法:

[java]  view plain  copy
 
  1. public int climbStairs(int n) {  
  2.     int[] data = new int[n + 3];  
  3.     data[1] = 1;  
  4.     data[2] = 2;  
  5.     for (int i = 3; i <= n; i++) {  
  6.         data[i] = data[i - 2] + data[i - 1];  
  7.     }  
  8.     return data[n];  
  9. }  

你可能感兴趣的:(【Leetcode】Climbing Stairs)