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?

建表,查表法搞定,用递归会做重复工作,效率不高。

public static int climbStairs(int n) {
        int[] result = new int[1000];
        result[0] = 0;
        result[1] = 1;
        result[2] = 2;
        for (int i = 3; i < result.length; i++) {
            result[i] = result[i - 1] + result[i - 2];
        }
        return result[n];
    }

你可能感兴趣的:(LeetCode)