Java [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 1 or 2 steps. In how many distinct ways can you climb to the top?
解题思路:
利用DP的方法,一个台阶的方法次数为1次,两个台阶的方法次数为2个。n个台阶的方法可以理解成上n-2个台阶,然后2步直接上最后一步;或者上n-1个台阶,再单独上一步。
代码如下:
public class Solution {
public int climbStairs(int n) {
int p2 = 1, p1 = 2, current = 0;
if(n <= 2)
return n;
for(int i = 2; i < n; i++){
current = p2 + p1;
p2 = p1;
p1 = current;
}
return current;
}
}
Java [Leetcode 70]Climbing Stairs的更多相关文章
- 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 解 ...
- 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 ...
- [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 爬楼梯
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 ...
- LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...
- [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爬楼梯 (C++)
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
随机推荐
- Mysql登录异常的一个问题:
问题描述: 正常在启动mysql的时候是这样的: 1. win打开输入界面: 2. 输入cmd命令,鼠标右键选择“以管理员身份运行”: 3, 之后再在界面输入登录mysql的登录语句: mysq -u ...
- cf 363A B C
A水题 ~~ 注意0输出 /************************************************************************* > Author ...
- Java学习第一篇:变量,数据类型,运算符,流程控制(简介)
一.安装和配置jdk 1.jdk是什么? (1).jdk全称是Java Development Kit, Java开发工具包; (2).jdk是sun公司开发的; (3).jdk主要包括:jre(Ja ...
- Chp10: Scalability and Memory Limits
The Step-by-Step Approach break down a tricky problem and to solve problems using what you do know. ...
- java基础知识回顾之---java String final类普通方法
辞职了,最近一段时间在找工作,把在大二的时候学习java基础知识回顾下,拿出来跟大家分享,如果有问题,欢迎大家的指正. /* * 按照面向对象的思想对字符串进行功能分类. * ...
- 读写txt文件
public void SetUpdateTime(string strNewDate) { try { var path =Application.StartupPath + Configurati ...
- *[codility]MinAvgTwoSlice
https://codility.com/demo/take-sample-test/min_avg_two_slice 此题要求一个数组子段的最小的平均数(返回第一个数字的index).刚开始想记录 ...
- Linux多线程编程和Linux 2.6下的NPTL
Linux多线程编程和Linux 2.6下的NPTL 在Linux 上,从内核角度而言,基本没有什么线程和进程的区别--大家都是进程.一个进程的多个线程只是多个特殊的进程他们虽然有各自的进程描述结构, ...
- HTML5入门3---视频播放器
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...
- ios开发--27个提升效率的iOS开源库推荐
超长慎入列表: DZNEmptyDataSet(UI,空表格视图解算器) PDTSimpleCalendar(UI,drop-in日历组件) MagicalRecord(实施活跃记录模式的Core D ...