• [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:

  • 相关阅读:
    HDU
    C# Stopwatch
    RMQ(Range Minimum Query)问题(转)
    HDU
    POJ
    HDU
    POJ
    POJ
    docker安装testlink
    廖雪峰Java2面向对象编程-3继承和多态-2多态
  • 原文地址:https://www.cnblogs.com/freeneng/p/3087963.html
Copyright © 2020-2023  润新知