【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 ...
随机推荐
- Apache Shiro(四)-登录认证和权限管理WEB支持(Servlet)
新建web项目 web.xml 修改web.xml,在里面加了个过滤器. 这个过滤器的作用,简单的说,就是 Shiro 入门里的TestShiro 这部分的工作,悄悄的干了. //加载配置文件,并获取 ...
- [转] Spring Integration 系统集成
[From] http://blog.csdn.net/w_x_z_/article/details/53316618 pring Ingegration 提供了基于Spring的EIP(Enterp ...
- spring4.x hibernate4.x 整合 ehcache 注解 annotate
[From] http://my.oschina.net/alexgaoyh/blog/348188
- JDK安装以及maven部署
JDK安装 检查原有JDK rpm -qa|grep jdk 假如原环境安装有JDK,卸载,命令举例: yum -y remove java--openjdk-headless-.b17.el7.x8 ...
- TensorFlow-简单的卷积神经网络
先弄懂卷积神经网络的原理,推荐这两篇博客:http://blog.csdn.net/yunpiao123456/article/details/52437794 http://blog.csdn. ...
- Docker容器日志查看与清理(亲测有效)
https://blog.csdn.net/yjk13703623757/article/details/80283729 1. 问题 docker容器日志导致主机磁盘空间满了.docker logs ...
- Python 字典的取值
不能用.取值 .是获取属性或方法 只能用中括号或者get方法 中括号和get中可以放字符串或者变量 get与[]的区别在于当key不存在,get不会报错,而且get可以设置取不到值时返回的默认值.
- 创建本地maven仓库
1.首先从下面地址下载nexus-oss-webapp-2.3.1-01 http://pan.baidu.com/s/1pKOLdbH 2.修改配置文件: 打开:*nexus-oss-webapp- ...
- Vim as a Python IDE
参考视频:http://v.youku.com/v_show/id_XNDY4NTM4NzY0.html 好的,在我们默认的centos6的操作系统中使用的python2,我们一般会再去安装一个pyt ...
- Hashtable和HashMap的区别,Properties类的简单使用
一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...