LeetCode 题解(226) : Climbing Stairs
来源:互联网 发布:清华 王奇 知乎 编辑:程序博客网 时间:2024/05/21 17:30
题目:
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?
题解:C++版:
class Solution {public: int climbStairs(int n) { if(n == 0 || n == 1) return n; int* ways = new int[n]; ways[0] = 1; ways[1] = 2; for(int i = 2; i <= n - 1; i++) ways[i] = ways[i - 1] + ways[i - 2]; return ways[n - 1]; }};
Java版:
public class Solution { public int climbStairs(int n) { if(n == 0 || n == 1) return n; int[] ways = new int[n]; ways[0] = 1; ways[1] = 2; for(int i = 2; i <= n - 1; i++) ways[i] = ways[i - 1] + ways[i - 2]; return ways[n - 1]; }}
Python版:
class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int """ if n == 0: return 0 if n == 1: return 1 step = [0] * n step[0], step[1] = 1, 2 i = 2 while i <= n - 1: step[i] = step[i - 1] + step[i - 2] i += 1 return step[n - 1]
0 0
- LeetCode 题解(226) : Climbing Stairs
- LeetCode题解:Climbing Stairs
- LeetCode题解:Climbing Stairs
- LeetCode题解:Climbing Stairs
- Leetcode题解 70. Climbing Stairs
- LeetCode 70 Climbing Stairs题解
- leetcode题解-70. Climbing Stairs
- LeetCode70. Climbing Stairs题解
- LeetCode 70. Climbing Stairs 题解 —— Java
- 【动态规划】Leetcode编程题解:70. Climbing Stairs
- LeetCode: Climbing Stairs
- LeetCode: Climbing Stairs
- [LeetCode]Climbing Stairs
- LeetCode Climbing Stairs
- [Leetcode] Climbing Stairs
- Leetcode: Climbing stairs
- LeetCode Climbing Stairs
- [LeetCode] Climbing Stairs
- Android Api Demos登顶之路(七十七)Graphics-->FingerPaint
- LeetCode 题解(224) : Merge Sorted Array
- LeetCode-Partition List
- Maximal Square
- LeetCode 题解(225) : Remove Duplicates from Sorted List
- LeetCode 题解(226) : Climbing Stairs
- js 计算输入框的字节数
- Solr 5.3安装和使用
- Bit Manipulation
- LeetCode 题解(227) : Add Binary
- *LeetCode-Number of 1 Bits
- js == 和 === 的不同 (两个等号 三个等号)
- 解密一个U盘
- LeetCode 题解(228) : Plus One