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 ...
随机推荐
- 在IDEA建立Maven的多模块Web项目
由于要搭建的是Maven项目,考虑到后面可能会有扩展,因此项目搭建的分模块的. 下面一步一步的来搭建这个项目 打开IDEA集成开发环境,点击File ---> New ---> Proje ...
- PowerBuilder -- 未公开函数
原文:http://blog.csdn.net/happymagic/article/details/51077322 @.已知一个DW中的某列的列名(在字符串变量中),以获得这个列对象的DWO 方法 ...
- java 发展简史
[0]README 0.1) 本文转自 core java volume 1,仅供了解Java 的发展历史,它的前世今生,所谓知己知彼,百战不殆(just a joke) : [1]java 发展简史 ...
- 内存MCE错误导致暴力扩充messages日志 以及chattr记录
由于放假,好久没登过服务器,今天登上服务器查看日志意外发现:/var/log/messages文件竟然被撑到20多个G!!!赶紧查看是什么情况,首先,20多个G的文件根本无法查看,因此,我想到了spl ...
- XShell 连接 vm虚拟机中的redhat Linux系统
选择的是nat链接,因为nat链接是没有网络的情况下,也是可以链接操作的,当然bridge也可以,那我就从第一步开始; 因为有的人可能改过电脑上的虚拟适配器的ip地址,导致和虚拟机默认的不一样了.如果 ...
- django url匹配过程
ROOT_URLCONF root URLconf module urlpatterns “include” other URLconf modules chops off whatever part ...
- Python的pymysql模块
PyMySQL是在Python3.x版本中用于连接MySQL服务器的一个库,Python2中则使用MySQLDB. 1.基本语法 # 导入pymysql模块 import pymysql # 连接da ...
- 小程序开发之xxx is not defined
遇到问题 在小程序开发中直接在函数中调用data中的变量直接赋值给新的变量,就会出现如下错误 VM33895:1 thirdScriptErrorapaymoney is not defined; [ ...
- Flask中的CBV和上下文初步解读
一 . flask中的CBV 相对于Django中的CBV,让我们来看看flask中的CBV是如何实现的 ? from flask import Flask, render_template, url ...
- Data Structure Binary Tree: Level order traversal in spiral form
http://www.geeksforgeeks.org/level-order-traversal-in-spiral-form/ #include <iostream> #includ ...