Python3解leetcode Maximum SubarrayClimbing 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个台阶,一共有多少种走法。
明显的动态规划问题,
当要到达地n个台阶的时候,有两种走法,第一种是从N-1个台阶走一步,第二种是N-2个台阶走两步。
class Solution:
def climbStairs(self, n: int) -> int:
if n ==1 :return 1
count= [0 for i in range(n)]
count[0] = 1
count[1] = 2
for i in range(2,n):
count[i] = count [i-1] + count[i-2]
return count[n-1]
Python3解leetcode Maximum SubarrayClimbing Stairs的更多相关文章
- Python3解leetcode Maximum SubarrayHouse Robber
问题描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
- Python3解leetcode Maximum Subarray
问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...
- Python3解leetcode Min Cost Climbing Stairs
问题描述: On a staircase, the i-th step has some non-negative cost cost[i]assigned (0 indexed). Once you ...
- Python3解leetcode Best Time to Buy and Sell Stock II
问题描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- Python3解leetcode N-ary Tree Level Order Traversal
问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
- Python3解leetcode Linked List Cycle
问题描述: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given link ...
- Python3解leetcode Single Number
问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...
- Python3解leetcode Same TreeBinary Tree Level Order Traversal II
问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
随机推荐
- JVM、垃圾回收、内存调优、常见參数
一.什么是JVM JVM是Java Virtual Machine(Java虚拟机)的缩写.JVM是一种用于计算设备的规范.它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现 ...
- 杂谈:HTML 5的消息通知机制
译文来源:http://www.ido321.com/1130.html 原文:HTML 5 Notification 译文:HTML 5 的消息通知机制 译者:dwqs watermark/2/te ...
- 巧用Excel提高工作效率
程序员如何巧用Excel提高工作效率 主要讲解下Excel中VLOOKUP函数的使用,相比于上一篇中的内容,个人觉得这个相对高级一些. 1.使用背景 为什么会使用到这个函数呢,背景是这样的,有两个系统 ...
- 循序渐进学Python 1 安装与入门
1 安装 2 使用 2.1 运行程序 3 艺搜参考 by 2013年10月16日 安装 Windows安装版,源码,帮助文档: 使用 打开开始菜单中的Python GUI启动Python解释器: 启动 ...
- 3_Jsp标签_简单标签_防盗链和转义标签的实现
一概念 1防盗链 在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿链接到当前的网页或文件,通过referer,网站可以检测目标网页访问的来源网页.有了referer跟踪来 ...
- 开始使用gradle
前提配置gradle环境 每个gradle构建都是以一个脚本开始的.gradle构建默认的名称为build.gradle.当在shell中执行gradle命令时,gradle会去寻找为build.gr ...
- 【BZOJ3834】[Poi2014]Solar Panels 分块好题
[BZOJ3834][Poi2014]Solar Panels Description Having decided to invest in renewable energy, Byteasar s ...
- 【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)
[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依 ...
- 解决因 gtx 显卡而导致的 google chrome 颜色显示不正常。色彩变淡发白,其实很简单
笔者因为换了用 gtx 1050 显卡替换了原来的集显. 导致chrome浏览器渲染颜色变淡而且泛白. 查了下肯能是因为换了显卡,没换高清显示器. 导致chrome自动启用了 dispaly p3 d ...
- js闭包实际用途
闭包例:防止双击 在线商店的购物车里,为防止“多重购买”,需要防止按钮被双击. 下面用“jQuery + 闭包”来实现这一功能. HTML <form name="frm" ...