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 ...
随机推荐
- wp8.1 Study14 FilePicker简单介绍
一.FileOpenPicker/FileSavePicker介绍 这个在使用手机中是十分经常的,如在朋友圈中你要发图片,首先点击添加图片,而后你会进入手机图片区,选择图片后自动返回朋友圈准备发图界面 ...
- Rhel7的基本使用
1.修改主机名 [root@localhost ~]# cat /etc/hostname localhost.localdomain[root@localhost ~]# hostnamectl s ...
- C++全局变量的声明和定义
(1)编译单元(模块) 在VC或VS上编写完代码,点击编译按钮准备生成exe文件时,编译器做了两步工作: 第一步,将每个.cpp(.c)和相应的.h文件编译成obj文件: 第二步,将工程中所有 ...
- 2013年7月份第1周51Aspx源码发布详情
启睿网络信息服务器实例源码 2013-7-5 [ VS2005 ]功能介绍:睿网络信息服务器,QiRui Net Information Services简称QRNIS,该软件前身系KCIS.当前版 ...
- ArrayList的使用方法【转载】
*** Source URL: http://i.yesky.com/bbs/jsp/view.jsp?articleID=889992&forumID=150 *** 1.什么是ArrayL ...
- hdoj-2031
#include "stdio.h"#include "stdlib.h"int main(){ char a[]={'0','1','2','3','4',' ...
- windows程序设计笔记
2014.05.06 新建一个visual C++ -- 常规 -- 空白 的项目,用.c后缀名指定这是一个用C语言来写的windows项目.和C语言的hellworld程序做了一个比较,按照wind ...
- C语言快速排序
复习快速排序,用C语言实现: #include <stdio.h> int quicksort(int begin, int end, int a[], int len); void ma ...
- 一些需要注意的C知识点
1.数组在作为参数传递到函数时,会退化为一个指针.也就是说,一旦进入函数内部,数组已经变为了一个指针.其实是在参数传递的时候进行了浅拷贝,编译器会声明一个指针指向该数组,在函数内部所有的操作都是对该临 ...
- STL源码分析《3》----辅助空间不足时,如何进行归并排序
两个连在一起的序列 [first, middle) 和 [middle, last) 都已经排序, 归并排序最核心的算法就是 将 [first, middle) 和 [middle, last) 在 ...