爬楼梯

原题

  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].spa

代码实现

算法实现类,解法一.net

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

算法实现类,解法二code

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;
    }
}
相关文章
相关标签/搜索