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 ...
随机推荐
- 使用jquery改动表单的提交地址
基本思路: 通过使用jquery选择器得到相应表单的jquery对象,然后使用attr方法改动相应的action 演示样例程序一: 默认情况下,该表单会提交到page_one.html 点击butto ...
- python学习(五)列表
#!/usr/bin/python # 列表的学习, 列表的概念不陌生, 就是熟悉一下python中的列表是如何操作的 # 1. 序列的操作 L = [ 123, 'spam', 1.23] # 里面 ...
- Perl语言学习笔记 15 智能匹配与give-when结构
1.智能匹配操作符 替代绑定操作符: 在哈希中查找某一个键: 比較两个数组是否全然同样: 查找列表中是否存在某个元素: 智能匹配操作符与顺序无关.~~ 左右元素能够互换 2.智能操作符优先级 3.gi ...
- netback的tasklet调度问题及网卡丢包的简单分析
近期在万兆网卡上測试,出现了之前千兆网卡没有出现的一个现象,tasklet版本号的netback下,vm进行发包測试,发现vif的interrupt默认绑定在cpu0上,可是vm发包执行时发现host ...
- 一套ui满足ios与android界面
1.android 画布宽高720*1280的标准来切图生成xhdpi标准图,后自动生成mdpi.hdpi.xhdpi.xxhdpi四套图. 2.ios画布宽高640 x 1136的标准来切图生成xh ...
- 认识 service worker
离线缓存可以提升用户体验,可以节省网络资源,但是,浏览器对资源缓存和自定义网络请求的控制一直不够完善,service worker 的出现就是为了解决这些问题 它可以解决目前离线应用的问题,同时也可以 ...
- Python学习总结之三 -- 优雅的字符串
优雅的字符串 前言 记得我在Python学习总结第一篇中有提到字符串,那个可以算是先打个招呼吧,因为没有提到任何关于字符串的处理方法.今天,给大家详细讲解一下Python中字符串的使用方法,如有不当或 ...
- python 基础 1.5 python 数据类型(一)--整型 浮点型 布尔型及字符串和常用方法
一.python 数据类型:数值,字符串,列表,元组,字典.以下操作是在linux 下 ipython中进行 1.数值 1>123 与 “123”的区别 答:123为数值,“123”在pyt ...
- php如何在原来的时间上加一天?一小时?
<?php echo "今天:",date('Y-m-d H:i:s'),"<br>"; echo "明天:",date( ...
- Coin和Token有什么区别
在币圈,经常可以听到“coin”和“token”这些词汇,他们究竟分别代表什么,有什么区别呢?下面本文就和大家一起来扒一扒. 什么是coin? coin (包括山寨coin)是一种数字货币,它通过加密 ...