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?
?
public class Solution { public int climbStairs(int n) { if (n <= 0) { return 0; } if (n == 1) { return 1; } else if (n == 2) { return 2; } int s1 = 1; int s2 = 2; int s3 = 1; for (int i = 3; i <= n; i++) { s3 = s1+s2; s1 = s2; s2 = s3; } return s3; } }
?
原文:http://hcx2013.iteye.com/blog/2222700