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?

Note: Given n will be a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
class Solution {
public int climbStairs(int n) {
if (n <= 2) {
return n;
}
int preOne = 2;
int preTwo = 1;
int sum = 0;
for (int i = 3; i <= n; i++) {
sum = preOne + preTwo;
preTwo = preOne;
preOne = sum;
}
return sum;
}
}

[LC] 70. Climbing Stairs的更多相关文章

  1. LN : leetcode 70 Climbing Stairs

    lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...

  2. 42. leetcode 70. Climbing Stairs

    70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...

  3. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  4. 377. Combination Sum IV 70. Climbing Stairs

    back function (return number) remember the structure class Solution { int res = 0; //List<List< ...

  5. Leetcode之70. Climbing Stairs Easy

    Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...

  6. 刷题70. Climbing Stairs

    一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...

  7. LeetCode练题——70. Climbing Stairs

    1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...

  8. [LeetCode] 70. Climbing Stairs 爬楼梯问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  9. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

随机推荐

  1. 苹果下架2.5万赌博APP!一场净化风暴正在迅速成型

    当下智能手机发展得如火如荼,但对于大众来说,体验终究还是要落到包罗万千的APP上.APP身为智能手机的灵魂,全面渗入了大众的工作.生活.娱乐.学习等多个方面.每一个APP的背后,其实都在打开着一扇通往 ...

  2. 系统学习python第五天学习笔记

    1.列表补充 extend() li = ["alex", "WuSir", "ritian", "barry", &q ...

  3. priority_queue(优先队列)的用法(包括pbds)

    置顶!!! 有时候在定义的时候,不要把两个<>连在一起写,以免被编译器错误理解!!!! 头文件 #include <queue> queue的一般用法不再叙述 类型名 prio ...

  4. php IP地址转换

    <?php $enip = ip2long('210.110.11.49); echo $enip."<br />";//-764540111 echo long ...

  5. ORB-SLAM2的编译运行以及TUM数据集测试

    ORB-SLAM2的编译运行以及TUM数据集测试 徐大徐 2018.02.06 17:04 字数 1838 阅读 2167评论 0喜欢 2 近段时间一直在学习高翔博士的<视觉SLAM十四讲> ...

  6. object detection模型转换成TensorFlow Lite,在Android应用

    环境 tensorflow = 1.12.0 bazel = 0.18.1 ubuntu = 16.04 python = 3.6.2 安装 bazel (0.18.1) 如果tensorflow是1 ...

  7. java生成图形验证码

    效果图 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Buf ...

  8. document.write的时机

    document.write第一次加载进入页面的时候会紧跟文档,写入内容.但是如果在文档已经加载完毕之后,再通过点击的方式调用函数的话会直接把整个文档覆盖掉.

  9. Python笔记_第三篇_面向对象_9.Python中的"get"和"set"方法(@property和@.setter)

    1. 限制访问的问题: 如果学过C# 语言的我们可以知道C# 语言有get和set方法.我们之前想要获取父类中的私有变量,只能通过写一个set和get的函数进行访问或者通过类生成的新关键字来访问私有属 ...

  10. Python笔记_第二篇_面向过程_第二部分_3.模块的概述

    这部分内容是非常重要的,分模块的基本概念和一些常用模块的使用,其实常用模块使用这部分也不是太全面,后续或者有机会再通过其他材料进行讲解. 1. 模块的概述: 目前代码比较少,写在一个文件中还体现不出什 ...