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?
代码如下(方法一:超时):
public class Solution {
public int climbStairs(int n) {
if(n==1||n==2||n==3)
return n; return climbStairs(n-1)+climbStairs(n-2);
}
}
方法二:AC
public class Solution {
public int climbStairs(int n) {
if(n==1||n==2)
return n; int num1=1,num2=2,sum=0;
for(int i=3;i<=n;i++)
{ sum=num1+num2;
num1=num2;
num2=sum;
}
return sum;
}
}
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 解 ...
- 377. Combination Sum IV 70. Climbing Stairs
back function (return number) remember the structure class Solution { int res = 0; //List<List< ...
- 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 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 ...
- [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 ...
随机推荐
- NOIP 2013 提高组 day1 T2 火柴排队 归并 逆序对
描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑i=1n(ai−bi)2∑i=1n(ai−bi) ...
- Word2013可以写博客
步骤如下http://www.cnblogs.com/guyichang/p/4629211.html
- [C/C++]C++标准
本文若如特别说明都引于ISO/IEC 14882:2011 7.声明(Declarations) 声明序列(declaration-seq): 声明(declaration) 声明序列(d ...
- bjui给出的一个标准应用的首页
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- Android 导入jar包 so模块--导入放置的目录
Android视图下 app目录下的jniLibs 对应Project 视图app下的libs目录 把jar包或者带so文件的文件夹(一般以处理器型号命名如:arbeabi arm64-v8a)复制 ...
- Visual Studio 2013 Update 2 and with Update 2
Microsoft 的开发工具 Visual Studio 2013 迎来 Update2 更新.本次更新将为普通开发者带来更多全新功能.修复之前旧版 Bugs.提升性能以及稳定性.之前已经安装 VS ...
- 在VS2010中打开VS2012的项目
修改工程文件来把VS2012的工程文件移植到VS2010中 首先是修改解决方案文件(.sln文件). 使用记事本打开,把里面的 Microsoft Visual Studio Solution Fil ...
- URL详谈
URL(Uniform Resource Locator,统一资源定位符)是地址的别名.它包含关于文件存储位置和浏览器应如何处理它的信息.互联网上的每个文件都有唯一的 URL. URL 的第一个部分称 ...
- POJ 2752 - Seek the Name, Seek the Fame (KMP)
题意:给一个字符串s,问s的某个前缀与后缀相同的情况时,长度是多少. 此题使用KMP的next数组解决. next数组中,j=next[i],next[i]表示S[0...i-1]的某个后缀(字符串S ...
- hello world Firmware Library
其实正点原子有良好的模板工程...user .lab文件 ,obj. 一脸蒙的是库函数的操作方式.... 为了便于管理,该项目文件夹内,我分了以下几个文件夹:"PROJ"存放工程文 ...