leetcode--Climbing Stairs

来源:互联网 发布:网天概预算软件 编辑:程序博客网 时间:2024/06/10 12:58

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?

[java] view plain copy
  1. public class Solution {  
  2.     public int climbStairs(int n) {  
  3.         if(n==1return 1;  
  4.         int[] res = new int[2];  
  5.         res[0] = 1;  
  6.         res[1] = 2;  
  7.         while(n>2){        
  8.             int t = res[1];  
  9.             res[1] += res[0];  
  10.             res[0] = t;  
  11.             n--;  
  12.         }  
  13.         return res[1];  
  14.     }  

原文链接http://blog.csdn.net/crazy__chen/article/details/46376009

原创粉丝点击