Data Structures and Algorithms [] (e) recursive

LeetCode

Climbing stairs  

Suppose you are climbing stairs. You need  n  order to reach your roof.

Every time you can climb one or two steps. How many different ways can climb to the roof of it?

Note: Given  n  is a positive integer.

 int climbStairs(int n) {
     
      if (n == 1) return 1;
      if (n == 2) return 2;

      int ret = 0;
      int pre = 2;
      int prepre = 1;
      for (int i = 3; i <= n; ++i) {
        ret = pre + prepre;
        prepre = pre;
        pre = ret;
      }
      Return the right; 
    }

 

Guess you like

Origin www.cnblogs.com/jiwen/p/11403789.html