【Leetcode】【Easy】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?
解题:
简单的运算后,可知此题为斐波那契数列生成题。
解题步骤:
1、新建三个初始变量step1 = 1,step2 = 2,result;
2、注意算法从n = 3开始,所以当n < 3时,直接返回结果。
class Solution {
public:
int climbStairs(int n) {
int result = ;
int stepOne = ;
int stepTwo = ; if (n == || n == )
return n; while (n-- && n >= ) {
result = stepOne + stepTwo;
stepOne = stepTwo;
stepTwo = result;
} return result;
}
};
附录:
斐波那契以及其他有趣数学数列
【Leetcode】【Easy】Climbing Stairs的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【Leetcode_easy】746. Min Cost Climbing Stairs
problem 746. Min Cost Climbing Stairs 题意: solution1:动态规划: 定义一个一维的dp数组,其中dp[i]表示爬到第i层的最小cost,然后来想dp[i ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【Leetcode】746. Min Cost Climbing Stairs
题目地址: https://leetcode.com/problems/min-cost-climbing-stairs/description/ 解题思路: 官方给出的做法是倒着来,其实正着来也可以 ...
- 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【easy】746. Min Cost Climbing Stairs 动态规划
On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you pay t ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
随机推荐
- HDU - 1525 博弈 暴力分析
先来看看比较显然的几个局面 (a,0) 先手必败 (a,a) 先手必胜 (a,ak) 先手必胜 (a,ak+r),k>1 先手必胜,因为先手有主动权把(a,r)让给后手或留给自己 对于开局(a, ...
- 启用和禁用TCPIP上的Netbios
'设置传输值1是启用,设置2为禁用 On Error Resume Next strComputer = "." Set objWMIService = GetObject(&qu ...
- centos7升级firefox的flash插件
1. 在https://get.adobe.com/flashplayer/下载文件.选择.tar.gz. 2. 下载的文件名为flash_player_npapi_linux.x86_64.tar. ...
- css引入 以及选择器040
css的介绍: css(Cascading Style Sheet) 层叠样式表 作用就是给HTML页面标签议案家各种样式 定义网页效果 简单来说 就是讲网页内容和显示样式进行分离 , 提高了显示功 ...
- 多租户概念以及FreeLink多租户设计思想
多租户实现思想 多租户技术的实现重点,在于不同租户间应用程序环境的隔离(application context isolation)以及数据的隔离(data isolation),以维持不同租户间应用 ...
- Vue 项目启动抛出 Error/ No PostCSS Config found in
项目启动时抛出 Error: No PostCSS Config found in … 的错误表示某个 css 文件不能被引入 解决办法: module.exports = { plugins: { ...
- 爬虫beautifulsoup实践
爬虫beautifulsoup实践: 目的:在https://unsplash.com/上爬取图片并保存到本地文件夹里. 一.观察response.首先,在Chrome浏览器里观察一下该网页的re ...
- 0325img和超链接的学习
上午学习了cellspacing\cellpadding,img标签以及三种属性,src,alt,title.下午学习了超链接<a></a>,绝对路径与相对路径,锚点链接的使用 ...
- matlab遍历文件制作自己的数据集 .mat文件
原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/9115788.html 看到深度学习里面的教学动不动就是拿MNIST数据集,或者是IMGPACK ...
- Wireshark使用技巧
Wireshark使用技巧 在分析网络时,包应该尽量的小,只要能定位问题即可. 1. 只抓包头,在wireshark中可以设置抓包大小. 如果使用tcpdump命令: [root@server_1 / ...