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 ...
随机推荐
- netbeans xdebug
xdebug配置 装了wamp后,xdebug默认就安装好了,为了能够用netbeans远程调试,配置文件里得加几句 [xdebug] xdebug.remote_enable = on xdebug ...
- 有两个好友A和B,住在一片长有蘑菇的由n*m个方格组成的草地,A在(1,1),B在(n,m)。现在A想要拜访B,由于她只想去B的家,所以每次她只会走(i,j+1)或(i+1,j)这样的路线,在草地上有k个蘑菇种在格子里(多个蘑菇可能在同一方格),问:A如果每一步随机选择的话(若她在边界上,则只有一种选择),那么她不碰到蘑菇走到B的家的概率是多少?
第二种方法:首先分析题意,可用概率的方法来计算,做了好几道百度的题目,觉得大多数是再考概率论,所以首先要弄懂题意,最后做题前把公式写出来,这样编码时才能游刃有余. 本题中下面的第一种用迭代枚举的方法来 ...
- 深度解析 | 秒懂AI+智慧手机实践
阅读数:17 随着人工智能的概念越来越深入人心,智慧化生活和对应的智慧化终端体验也吸引越来越多的目光.可以想见,人工智能会深刻改变终端产业,但目前也面临各种挑战和问题.此前,在南京软件大会上,华 ...
- programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation
编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...
- Darwin做直播时对ReflectorSession引用数的控制
在之前的博客中,我们提到了如何用Darwin&live555实现直播过程,那么更进一步,当直播结束时,我们需要关闭所有正在收看的客户端,并且delete转发会话ReflectorSession ...
- Mybatis之增删改查操作
准备工作 建立整体项目目录 新建一个java工程,创建如下工程目录 其中com.kang.pojo中存放pojo类,com.kang.test中存放测试类. 源码目录config中存放Mybatis的 ...
- iOS 流布局 UICollectionView使用(使用FlowLayout进行更灵活布局)
在UICollectionView的布局中,如果每个item的大小都一样那么是十分简单的事情,但是,如果我们想要的每个item大小不一样呢,这个时候,就要对UICollectionViewFlowLa ...
- django 设置静态文件,static
django 设置静态文件,static 一.搜集静态文件 1.1 命令行查看 collectstatic guoguos-MacBook-Pro:mysite guoguo$ python mana ...
- Linux入门基础(二)——cp、nano命令
- MySql索引建立规则
为了使索引的使用效率更高,在创建索引时,必须考虑在哪些字段上创建索引和创建什么类型的索引.本小节将向读者介绍一些索引的设计原则. 1.选择唯一性索引 唯一性索引的值是唯一的,可以更快速的通过该索引来确 ...