【Leetcode】Climbing Stairs

题目链接:https://leetcode.com/problems/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?

思路:

上一步怎么走的跟下一步都的没关系。用data数组表示到每一步为止,有多少种方法。

算法:

	public int climbStairs(int n) {
		int[] data = new int[n + 3];
		data[1] = 1;
		data[2] = 2;
		for (int i = 3; i <= n; i++) {
			data[i] = data[i - 2] + data[i - 1];
		}
		return data[n];
	}


你可能感兴趣的:(【Leetcode】Climbing Stairs)