zoukankan      html  css  js  c++  java
  • [Leetcode 29] 70 Climbing Stairs

    Probelm:

    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?

    Analysis:

    Simplest DP problem, 

    Sn = Sn-1 + Sn-2 with S1 = 1 and S2 = 2;

    Code:

     1 class Solution {
     2 public:
     3     int climbStairs(int n) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         if (n == 1) return 1;
     7         if (n == 2) return 2;
     8         
     9         int s1=1, s2=2, s3;
    10         
    11         for (int i=2; i<n; i++) {
    12             s3 = s1 + s2;
    13             s1 = s2;
    14             s2 = s3;
    15         }
    16         
    17         return s3;
    18     }
    19 };
    View Code

    Attention:

  • 相关阅读:
    子网划分详解
    USACO range
    USACO shopping
    USACO fence
    USACO Spinning Wheels
    USACO butter
    USACO msquare
    USACO Feed Ratios
    USACO Stringsobits
    USACO Factorials
  • 原文地址:https://www.cnblogs.com/freeneng/p/3087963.html
Copyright © 2011-2022 走看看