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. PL/SQL存储过程

    一.概述 过程和函数统称为PL/SQL子程序,他们是被命名的PL/SQL块,均存储于数据库中. 并通过输入.输出和输入输出参数与其调用者交换信息.唯一区别是函数总向调用者返回数据. 二.存储过程详解 ...

  2. ping 原理与ICMP协议

    ping 的原理     ping 程序是用来探测主机到主机之间是否可通信,如果不能ping到某台主机,表明不能和这台主机建立连接.ping 使用的是ICMP协议,它发送icmp回送请求消息给目的主机 ...

  3. xcode 查看stastic

    点GPU 双击柱状图 从上面list里点performance

  4. js字符串解析与转换成数字

    解析允许字符串中含有非法数字字符,解析按从左至右的顺序,如果遇到非数字字符就停止.而转换不允许出现非数字字符,否则会失败并返回NaN

  5. CNN模型合集 | 1 LeNet

    1.1 LeNet的设计思想 1998年LeCun提出,经典结构,3层,五脏俱全(卷积层.Pooling层.FC网络.Sigmod层),对标传统神经网络.主要设计贡献 局部感受野(local rece ...

  6. ID生成算法(一)——雪花算法

    JavaScript生成有序GUID或者UUID,这时就想到了雪花算法. 原理介绍: snowFlake算法最终生成ID的结果为一个64bit大小的整数,结构如下图: 解释: 1bit.二进制中最高位 ...

  7. c 输出是自动显示输出类型

    显示0x i= print("%#x\n",i) 显示6位有效数字 i= print("l=%.6lf\n",i)

  8. 使用Spring Ehcache二级缓存优化查询性能

    最近在对系统进行优化的时候,发现有些查询查询效率比较慢,耗时比较长, 通过压测发现,主要耗费的性能 消耗在 查询数据库,查询redis 数据库:连接池有限,且单个查询不能消耗大量的连接池,占用大量IO ...

  9. 小程序tabBar的使用

    这个selectedIconPath一定要写,否则选中的那个图片是不会显示的 下面是不写的现象: onTabItemTap的使用---下面的现象说明:只有tab值向哪个页面才会触发.

  10. BZOJ3781小B的询问

    莫队裸题. 维护的时候有的打法是利用(a-1)^2==a^2-2*a+1转移,也可以,但是通用性不太够. 下面的打法就是先把这个点的贡献删掉,然后更新这个点,再把这个点的贡献加回来,这种解法更加通用一 ...