【Leetcode】【Easy】Climbing Stairs
2015-01-14 04:06
387 查看
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?
解题:
简单的运算后,可知此题为斐波那契数列生成题。
解题步骤:
1、新建三个初始变量step1 = 1,step2 = 2,result;
2、注意算法从n = 3开始,所以当n < 3时,直接返回结果。
附录:
斐波那契以及其他有趣数学数列
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
解题:
简单的运算后,可知此题为斐波那契数列生成题。
解题步骤:
1、新建三个初始变量step1 = 1,step2 = 2,result;
2、注意算法从n = 3开始,所以当n < 3时,直接返回结果。
class Solution { public: int climbStairs(int n) { int result = 0; int stepOne = 1; int stepTwo = 2; if (n == 1 || n == 2) return n; while (n-- && n >= 2) { result = stepOne + stepTwo; stepOne = stepTwo; stepTwo = result; } return result; } };
附录:
斐波那契以及其他有趣数学数列
相关文章推荐
- LeetCode-70-Climbing Stairs(动态规划)-Easy
- leetcode 70 Climbing Stairs(难易度:Easy)
- [Leetcode 70, easy] Climbing Stairs
- leetcode 070 Climbing Stairs(难易度:Easy)
- 【leetcode】Climbing Stairs (easy)
- 【Leetcode-Easy-70】Climbing Stairs
- 【LeetCode】(70)Climbing Stairs (Easy)
- Leetcode:Climbing Stairs
- Leetcode-Database-176-Second Highest Salary-Easy(转)
- 动态规划——Climbing Stairs(easy,python)
- Leetcode_Easy_Medium题集
- LeetCode 070 Climbing Stairs
- [LeetCode]Climbing Stairs
- Leetcode 620. Not Boring Movies(Easy)
- Leetcode 389. Find the Difference (Easy) (cpp)
- Leetcode53[easy]--Maximum Subarray
- [Leetcode 36, easy] Valid Sudoku
- Leetcode 438. Find All Anagrams in a String (Easy) (cpp)
- Leetcode 26. Remove Duplicates from Sorted Array (Easy) (cpp)
- LeetCode - easy-695. Max Area of Island