弟中弟的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?

Note: Given n will be a positive integer.

Example 1:
Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:
Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

思路

开始的时候想的是用回溯法进行递归,但是尝试提交了一次后发现超时了。因此想到了动态规划。
除了第一阶和第二阶楼梯以外,上到第n阶台阶的方法a(n)只有两种可能:从a(n-1)迈一步上去或者从a(n-2)迈两步上去。因此得到了状态转移方程a[i]=a[i-1]+a[i-2],且a[1]=1, a[2]=2.

代码(C)

int climbStairs(int n) {int ans[1000];ans[1]=1;ans[2]=2;for(int i=3;i<=n;i++){ans[i]=ans[i-1]+ans[i-2];}return ans[n];
}

弟中弟的Leetcode总结——数组类(十)相关推荐

  1. 弟中弟的Leetcode总结——数组类(十一)

    弟中弟的Leetcode总结--数组类(十一) 题目描述 Single Number Given a non-empty array of integers, every element appear ...

  2. 弟中弟的Leetcode总结——数组类(五)

    弟中弟的Leetcode总结--数组类(五) 题目描述 Remove Duplicates from Sorted Array Given a sorted array nums, remove th ...

  3. 弟中弟的Leetcode总结——数组类(二)

    弟中弟的Leetcode总结--数组类(二) 题目描述 Container With Most Water Given n non-negative integers a1, a2, -, an , ...

  4. 弟中弟的Leetcode总结——数组类(四)

    弟中弟的Leetcode总结--数组类(四) 题目描述 Two Sum Given an array of integers, return indices of the two numbers su ...

  5. 弟中弟的Leetcode总结——数组类(八)

    弟中弟的Leetcode总结--数组类(八) 题目描述 Combination Sum Given a set of candidate numbers (candidates) (without d ...

  6. 弟中弟的Leetcode总结——数组类(九)

    弟中弟的Leetcode总结--数组类(九) 题目描述 Subsets Given a set of distinct integers, nums, return all possible subs ...

  7. 弟中弟的Leetcode总结——数组类(六)

    弟中弟的Leetcode总结--数组类(六) 题目描述 3Sum Closest Given an array nums of n integers and an integer target, fi ...

  8. 弟中弟的Leetcode总结——数组类(七)

    弟中弟的Leetcode总结--数组类(七) 题目描述 Maximum Subarray Given an integer array nums, find the contiguous subarr ...

  9. 弟中弟的Leetcode总结——字符串类(一)

    弟中弟的Leetcode总结--字符串类(一) 题目描述 Longest Substring Given a string, find the length of the longest substr ...

最新文章

  1. 机器学习笔试题精选(二)
  2. FPGA设计思想之“逻辑复制”
  3. Google Map App 问题集锦
  4. jquery技巧(持续更新。。)
  5. github流程图_「强烈推荐」开源的在线流程图工具--draw.io
  6. VTK:vtkBoxWidget用法实战
  7. Linux命令执行顺序— ||和和; 比较
  8. iOS UITextField使用全攻略
  9. C++学习之路 | PTA(甲级)—— 1064 Complete Binary Search Tree (30分)(带注释)(精简)
  10. Jenkins+Gitlab+ansible-playbook上线流程
  11. mysql linux 手动安装mysql服务_Linux手动安装Mysql
  12. mysql5.6 主从 延迟_MySQL5.6升级5.7时,出现主从延迟问题排查过程
  13. 把“~/niunantest.aspx”转成http://localhost:4532/test/niunantest.aspx的方法
  14. K8s宣布弃用Docker?千万别慌!
  15. 流模型 操作 xml
  16. 基于KINECT的拖拉机防撞系统
  17. 金仓数据库KingbaseES表空间(tablespace)知多少
  18. csdn竟然还有这种神器!后悔没有早点知道!超好用的csdn插件,别再犹豫了,赶快入手吧!
  19. 【LSTM+embeddingbag】进行文本分类完整代码~
  20. 第三章 Hello World入门程序

热门文章

  1. renpy教程-全屏播放视频
  2. 什么是泛域名解析和泛域名解析配置方法
  3. Intel PinTools 安装使用教程
  4. (受益匪浅)-大四学长高项备考经验
  5. 计算机毕业设计Node.js+VueJava远程健康数据管理系统(程序+源码+LW+部署)
  6. bootstrap实现轮播图
  7. 西门子S7 200smart与欧姆龙E5cc温控器通讯程序
  8. Linux系统根目录下文件是啥
  9. 【20保研】北京大学前沿交叉学科研究院关于举办2019年交叉学科优秀大学生夏令营的通知...
  10. 根据IP定位城市名称/经纬度