[LC] 70. Climbing Stairs
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的更多相关文章
- 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 ...
- 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 ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- 377. Combination Sum IV 70. Climbing Stairs
back function (return number) remember the structure class Solution { int res = 0; //List<List< ...
- Leetcode之70. Climbing Stairs Easy
Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...
- 刷题70. Climbing Stairs
一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...
- LeetCode练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
- [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 ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- 用Axure画原型图有感
感觉前端做UE非常有优势啊- 但是在制作的时候,似乎陷入了误区: (1)只求原型图的漂亮,色彩丰富,忽略了其本质作用,是用来整理逻辑,画出逻辑流程的. (2)一开始就追求交互,高保真的原型,忽视了细节 ...
- MySQL--mysql中You can’t specify target table for update in FROM clause错误解决方法
参考:http://www.jb51.net/article/60926.htm mysql中You can't specify target table for update in FROM cla ...
- 明明办理的是100M光纤,为何经过路由器输出只有20M?
就在今年7月26日,宽带发展联盟发布了第20期<中国宽带速率状况报告>(2018年第二季度).报告显示,2018年第二季度我国固定宽带网络平均下载速率达到21.31Mbps,比去年第二季度 ...
- Python笔记_第四篇_高阶编程_正则表达式_2.正则表达式入门
1. 匹配单个字符和数字: . --->> 匹配除换行符以外的任意字符.[0123456789] --->> []字符集合,表示匹配方括号中所包含的任意一个字符.[Thomas ...
- 安装使用离线版本的维基百科(Wikipedia)
1 相关背景 平常大家在上网查询一些基本概念的时候常常会参考维基百科上面的资料,但是由于方校长研制的GFW(长城防火墙系统)强大的屏蔽功能,好多链接打开以后,不出意外会出现著名的“404NOT FOU ...
- 十、GUI编程
GUI图形用户界面编程 GUI编程类似“搭积木”,将一个个组件放到窗口中,并通过增加“事件处理”,完成一个个程序.例如:记事本.word.画图工具等. tkinter模块 tkinter是 ...
- VMware vSphere虚拟化-VMware ESXi 5.5组件添加本地磁盘--虚拟机扩容
本地存储器可以是位于ESXi主机内部的内部硬盘,也可以是位于主机之外并直接通过SAS或者SATA等协议连接在主机上的外部存储系统.本地存储不需要存储网络即可与主机进行通信,只需要一根连接到存储单元的电 ...
- 17.3.10--C语言运行的步骤
编译-->生成-->调试-->链接-->运行 编译就是:将你编写的C语言程序翻译成机器能识别运行的指令集 生成就是:根据编译完成的指令集制造出机器可以具体执行的指令序列 调试就 ...
- 第3章 ZooKeeper基本数据模型
第3章 ZooKeeper基本数据模型 3-1 zk数据模型介绍 3-2 zk客户端连接关闭服务端,查看znode ./zkCli.sh Ctrl + C 退出 =================== ...
- 03 Mybatis:01.Mybatis课程介绍及环境搭建&&02.Mybatis入门案例
mybatis框架共四天第一天:mybatis入门 mybatis的概述 mybatis的环境搭建 mybatis入门案例 -------------------------------------- ...