【LeetCode-面試演算法經典-Java實現】【070-Climbing Stairs(爬樓梯)】,leetcode--java

來源:互聯網
上載者:User

【LeetCode-面試演算法經典-Java實現】【070-Climbing Stairs(爬樓梯)】,leetcode--java
【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】

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.