【LeetCode-面试算法经典-Java实现】【070-Climbing Stairs(爬楼梯)】

【070-Climbing Stairs(爬楼梯)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  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?

题目大意

  你正在爬一个楼梯,要走n步才能到达顶部,每次你可以走两步或者一步,请问你有多少种不同的方法爬到顶部。

解题思路

  解法一:用组合数的思想求解,分下面的情况,没有一次走两个台阶的有C(0, n),只一次走两个台阶C(1, n-1),只二次走两个台阶,C(2, n-2),直到只有[n/2](向下取整)次走两个台阶。其和就是所有的解法。

  解法二:使用分治法,对n个台阶,用一个数组保存其解,a[1] = 1,a[2] = 2, k >= 2,有a[k] = a[k-1]+a[k-2].

代码实现

算法实现类,解法一

public class Solution {
    public int climbStairs(int n) {
        if (n < 0) {
            return 0;
        } else {
            int result = 0;
            for (int i = 0; i <= n; i++, n--) {
                result += combination(i, n);
            }
            return result;
        }
    }

    /**
     * 求组合数
     *
     * @param sup 上标
     * @param sub 下标
     * @return 结果
     */
    private int combination(int sup, int sub) {

        if (sup > sub || sup < 0 || sub < 0) {
            throw new RuntimeException("Error args");
        }

        if (sup == 0) {
            return 1;
        }

        if (sup > sub / 2) {
            sup = sub - sup;
        }

        long x = 1; // 分母的积
        long y = 1; // 分子的积
        long z;
        for (int i = 1; i <= sup; i++) {
            x *= (sub - i + 1);
            y *= i;
            z = gcd(x, y); // 找最大公约数
            // 分子分母都缩小最大公约数倍
            x /= z;
            y /= z;
        }

        return (int) (x / y);
    }

    private int gcd(long min, long max) {
        long tmp;
        if (max < min) {
            tmp = min;
            min = max;
            max = tmp;
        }

        while (max % min != 0) {
            tmp = min;
            min = max % min;
            max = tmp;
        }

        return (int) min;
    }
}

算法实现类,解法二

public class Solution {
    public int climbStairs(int n) {

        int result = 0;

        // 只有一阶
        if (n == 1) {
            result = 1;
        }
        // 只有两阶
        else if (n == 2) {
            result = 2;
        }
        // 楼梯阶数大于2
        else if (n > 2) {
            // 保存所有的解法
            int[] ways = new int[n];

            ways[0] = 1;
            ways[1] = 2;

            for (int i = 2; i < ways.length; i++) {
                ways[i] = ways[i - 1] + ways[i - 2];
            }

            result = ways[ways.length - 1];
        }

        return result;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

解法一

解法二

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47247995

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 02:08:02

【LeetCode-面试算法经典-Java实现】【070-Climbing Stairs(爬楼梯)】的相关文章

LeetCode | 0070. Climbing Stairs爬楼梯【Python】

LeetCode 0070. Climbing Stairs爬楼梯[Easy][Python][动态规划] Problem LeetCode 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

[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(Dynamic Programming)入门题. state: dp[i] 表示爬到第i个楼梯的所有方法的和function: dp[i] =

LeetCode Climbing Stairs 爬楼梯

递归法(TLE代码): 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 if(n==0) 5 return 1; 6 if(n<0) 7 return 0; 8 return (climbStairs(n-1)+climbStairs(n-2)); 9 } 10 }; 动态规划法: 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 if(n==1) return

[LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step w

Climbing Stairs 爬楼梯问题,每次可以走1或2步,爬上n层楼梯总方法 (变相fibnacci)

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? 当n=1时,有1种方法,即直接走1步 当n=2时,有2方法:连续走2步,或直接走两步 对于n,设f(n)为总方法,则 f(n) = f(n-1)+f(n-2) ps:f

Climbing Stairs爬楼梯——动态规划

题目描写叙述: 初阶:有n层的台阶,一開始你站在第0层,每次能够爬两层或者一层. 请问爬到第n层有多少种不同的方法? 进阶:假设每次能够爬两层.和倒退一层,同一个位置不能反复走,请问爬到第n层有多少种不同的方法? 解题思路: 初阶:一维动态规划.爬楼梯数目事实上是一个斐波拉契数列. 假定f[i] 表示是爬到第i层的方法,那么f[i] = f[i-1] + f[i-2] //第i层的方法数目等于第i-1层数目加上第i-2层数目. 初值:f[0] = 1, f[1] = 1. //最開始没有爬和第一

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example

【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either