[LeetCode]Climbing Stairs
2015-03-29 21:13
232 查看
今天TX笔试。感觉做的很一般,以前的知识都忘光了。另外得知投PC客户端方向的人数“较多”,心都凉了半截。
不过有一道简答题是LeetCode OJ上做过的。出来跟同学讨论,发现有人在这题上费了不少时间。
于是觉得LeetCode 确实挺有用的,毕竟都是些经典题,针对找工作要笔试面试的码农设计的。
目前LeetCode 上我是把Easy难度的都做完了,平时一边刷题一边会做些笔记。
今天开始,就把笔记po到这博客上来好了。以后会陆陆续续地更新。
LeetCode 上的那道题是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?
其实就是斐波那契数列了!
设f(n)是n步的总方法数。
则f(1)=1,f(2)=2,f(n)=f(n-1) + f(n-2)
不过有一道简答题是LeetCode OJ上做过的。出来跟同学讨论,发现有人在这题上费了不少时间。
于是觉得LeetCode 确实挺有用的,毕竟都是些经典题,针对找工作要笔试面试的码农设计的。
目前LeetCode 上我是把Easy难度的都做完了,平时一边刷题一边会做些笔记。
今天开始,就把笔记po到这博客上来好了。以后会陆陆续续地更新。
LeetCode 上的那道题是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?
其实就是斐波那契数列了!
设f(n)是n步的总方法数。
则f(1)=1,f(2)=2,f(n)=f(n-1) + f(n-2)
class Solution { public: int climbStairs(int n) { if (n == 1 || n == 2) { return n == 1 ? 1 : 2; } int a = 2, b = 1, c; for (int i = 0; i < n - 2; i++) { c = a + b; b = a; a = c; } return a; } };
相关文章推荐
- LeetCode 70 — Climbing Stairs(C++ Java Python)
- Climbing Stairs --- LeetCode
- 【Leetcode】【Easy】Climbing Stairs
- [LeetCode] Climbing Stairs, Solution
- leetcode-Climbing Stairs
- LeetCode-70-Climbing Stairs(爬楼梯)
- LeetCode——Climbing Stairs
- LeetCode70:Climbing Stairs
- leetcode 70 Climbing Stairs
- Climbing Stairs - LeetCode
- [LeetCode] Climbing Stairs
- LeetCode--No.70--Climbing Stairs
- leetcode-Climbing Stairs
- LeetCode-Climbing Stairs(爬楼梯问题)
- leetcode70- Climbing Stairs
- leetcode70-Climbing Stairs
- leetcode: Climbing Stairs
- 70.[Leetcode]Climbing Stairs
- Leetcode: Climbing Stairs
- [Leetcode] Climbing Stairs