zoukankan      html  css  js  c++  java
  • LeetCode 70. Climbing Stairs

    70. Climbing Stairs(两数之和)

    链接

    https://leetcode-cn.com/problems/climbing-stairs

    题目

    假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

    每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

    注意:给定 n 是一个正整数。

    示例 1:

    输入: 2
    输出: 2
    解释: 有两种方法可以爬到楼顶。
    1.  1 阶 + 1 阶
    2.  2 阶
    

    示例 2:

    输入: 3
    输出: 3
    解释: 有三种方法可以爬到楼顶。
    1.  1 阶 + 1 阶 + 1 阶
    2.  1 阶 + 2 阶
    3.  2 阶 + 1 阶
    

    思路

    算是一个递归方法,如果只有一阶那么只有一种方法,二阶有两种方法。
    递归方程
    f(n)=f(n-1)+f(n-2)

    代码:

      public int climbStairs(int n) {
        if (n < 3) {
          return n;
        }
        int f1 = 1;
        int f2 = 2;
        int fn = 0;
        for (int i = 3; i <= n; i++) {
          fn = f1 + f2;
          f1 = f2;
          f2 = fn;
        }
        return fn;
      }
    
  • 相关阅读:
    Python 简单总结
    Python 简单总结
    Python 简介
    Python基础题
    Python基础题
    tDQSS
    parameter–precharge, tRCD and tRAS
    parameter–key parameters
    parameter -- tWR
    命令集
  • 原文地址:https://www.cnblogs.com/blogxjc/p/12112760.html
Copyright © 2011-2022 走看看