70. Climbing Stairs

Easy

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
package leetcode.easy;

public class ClimbingStairs {
@org.junit.Test
public void test() {
int n1 = 2;
int n2 = 3;
System.out.println(climbStairs1(n1));
System.out.println(climbStairs1(n2));
System.out.println(climbStairs2(n1));
System.out.println(climbStairs2(n2));
System.out.println(climbStairs3(n1));
System.out.println(climbStairs3(n2));
System.out.println(climbStairs4(n1));
System.out.println(climbStairs4(n2));
System.out.println(climbStairs5(n1));
System.out.println(climbStairs5(n2));
System.out.println(climbStairs6(n1));
System.out.println(climbStairs6(n2));
} public int climbStairs1(int n) {
return climb_Stairs(0, n);
} public int climb_Stairs(int i, int n) {
if (i > n) {
return 0;
}
if (i == n) {
return 1;
}
return climb_Stairs(i + 1, n) + climb_Stairs(i + 2, n);
} public int climbStairs2(int n) {
int[] memo = new int[n + 1];
return climb_Stairs(0, n, memo);
} public int climb_Stairs(int i, int n, int memo[]) {
if (i > n) {
return 0;
}
if (i == n) {
return 1;
}
if (memo[i] > 0) {
return memo[i];
}
memo[i] = climb_Stairs(i + 1, n, memo) + climb_Stairs(i + 2, n, memo);
return memo[i];
} public int climbStairs3(int n) {
if (n == 1) {
return 1;
}
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
} public int climbStairs4(int n) {
if (n == 1) {
return 1;
}
int first = 1;
int second = 2;
for (int i = 3; i <= n; i++) {
int third = first + second;
first = second;
second = third;
}
return second;
} public int climbStairs5(int n) {
int[][] q = { { 1, 1 }, { 1, 0 } };
int[][] res = pow(q, n);
return res[0][0];
} public int[][] pow(int[][] a, int n) {
int[][] ret = { { 1, 0 }, { 0, 1 } };
while (n > 0) {
if ((n & 1) == 1) {
ret = multiply(ret, a);
}
n >>= 1;
a = multiply(a, a);
}
return ret;
} public int[][] multiply(int[][] a, int[][] b) {
int[][] c = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j];
}
}
return c;
} public int climbStairs6(int n) {
double sqrt5 = Math.sqrt(5);
double fibn = Math.pow((1 + sqrt5) / 2, n + 1) - Math.pow((1 - sqrt5) / 2, n + 1);
return (int) (fibn / sqrt5);
}
}

LeetCode_70. Climbing Stairs的更多相关文章

  1. [LeetCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  2. [LintCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  3. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  4. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  5. Climbing Stairs

    Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...

  6. 3月3日(6) Climbing Stairs

    原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...

  7. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

  8. 【LeetCode练习题】Climbing Stairs

    Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...

  9. 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 ...

随机推荐

  1. 神奇搜索算法A*

    A* A*是一种启发式搜索算法,又叫最佳图搜索算法. 何谓启发式搜索? 众所周知,计算机在执行搜索算法时是没开上帝视角的.因此,在搜索时,往往显得盲目,把所有可能的状态全部遍历,这种搜索我们统称盲目搜 ...

  2. 28-SQLServer带见证服务器的镜像搭建

    一.注意点 1.数据库的模式要是完整模式. 2.要对数据库完整备份和事务日志备份,分别还原到镜像库上,使用NORECOVERY模式. 3.镜像数据库是不允许删除和操作,即便查看属性也不行. 4.先删除 ...

  3. CSS float详解

    前言:在我们写CSS样式的时候,float,position,display,overflow这几个关键字用得比较多. 弄清楚他们之间的原理,我们可以更高效的写出我们想要的布局. 作者:Ry-yuan ...

  4. javascript权威指南第22章高级技巧

    HTML <!DOCTYPE html> <html> <head> </head> <body> <div style=" ...

  5. ES WIndows 安装 ES与ES-head

    一.ES的安装 1.到ES官网下载ES 安装ES前,需要安装JDK1.8以上版本 https://www.elastic.co/downloads/elasticsearch 2.解压ES 3.安装E ...

  6. bzoj 3999: [TJOI2015]旅游 LCT

    没啥难的,inf 的值设小了调了半天~ code: #include <bits/stdc++.h> #define N 50003 #define lson t[x].ch[0] #de ...

  7. 四川大学第二届SCUACM新生赛(同步赛)题解

    周末没事干,就不要脸地去一边吃饭一边看学弟沈阳拿银一边水了个比赛,水都水了,简单写个题解. 比赛链接 A,丁姐姐喜欢Fibonacci.签到1,斐波那契%3 1 1 0 1 1 0 1 1 0..., ...

  8. Gym - 102346G Getting Confidence 最小费用最大流

    Gym - 102346GGetting Confidence 题意:n*n的格子,每个格子上有一个数,要求每行每列都只能拿一个数,使得乘积最大,然后输出每列选择的是第几行的数. 如果是加法的话,那么 ...

  9. 静态blog的免费托管部署、加域名与搜索优化(SEO)

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/hugo_blog_host_and_seo 给博客加个域名准备长 ...

  10. 三十一、Gawk基础入门

    AWK:Aho Weinberger Kernighan awk :报告生成器.格式化文本输出 一.gawk - pattern scanning and processing language 基本 ...