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. JavaScript—飞机大战2版

    前面的思路对了  BUG 出在了计时器和没有加判断页面是否存在元素 <!DOCTYPE html> <html lang="en"> <head> ...

  2. CentOS8上用Docker部署开源项目Tcloud

    一.安装Docker 1.我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核3.8以上) root账户登录,查 ...

  3. LeetCode——714. 买卖股票的最佳时机含手续费.

    给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 :非负整数 fee 代表了交易股票的手续费用. 你可以无限次地完成交易,但是你每次交易都需要付手续费.如果你已经购买了一个 ...

  4. 为什么Web前端变的越来越复杂,变得更加难学了

    前端发展了也有些年头了,曾记得很多年前,聊起前端,都觉得是很简单,那个时候都没有前端工程师这个职位.可现在,前端已经逆袭了,已经不是原来的样子了,各种技术层出不穷,显的越来越高深莫测了.前端真的变得那 ...

  5. 吴裕雄--天生自然Linux操作系统:Linux 云服务器

    自己安装服务器还是麻烦了些,现在一般都推荐大家使用云服务器,比较方便,价格也不贵. 腾讯云 以下几款性价比非常高,有几款是需要抢购的,大家看好时间基本能拿到. 1.1核2G 99/年,可以用来学习,L ...

  6. flask汇总

    flask框架 蓝图 随着flask程序越来越复杂,我们需要对程序进行模块化的处理,之前学习过python的模块化管理,于是针对一个简单的flask程序进行模块化处理 Blueprint概念 简单来说 ...

  7. mysql 获取数据库和表结构信息

    SELECT * FROM information_schema.`TABLES` where TABLE_SCHEMA = '数据库名';SELECT * FROM information_sche ...

  8. Linux应用编程之lseek详解

    Linux应用编程之lseek详解 1.lseek函数介绍 (1).文件指针:当我们要对一个文件进行读写时,一定要先打开这个文件,所以我们读写的所有文件都是动态文件.动态文件在内存中的形态就是文件流的 ...

  9. mysql超大sql导入(10G)

    mysql  大数据库文件上传(10G) phpstudy2017 环境  mysql 5.5.53   php 5.5.45 更改php.ini memory_limit 为 2048M php.i ...

  10. 架构之道(5) - APP和Web的后台架构

    当一个项目,同时需要Web.手机H5.Android,三平台同时可以测览,那就需要很简洁而有力的架构. 而我这就经历了这麽一个项目,先开发网站,然后是手机H5,最后是Android. 自信男人,无须多 ...