Leetcode题解 70. Climbing Stairs

来源:互联网 发布:mac jmeter下载安装 编辑:程序博客网 时间:2024/05/01 11:46

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];    }
0 0
原创粉丝点击