leetcode-easy-dynamic-70 Climbing Stairs
mycode 65%
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 1
if n == 2:
return 2
i = 3
dic = {1:1,2:2}
while i < n+1:
dic[i] = dic[i-2] + dic[i-1]
i += 1
return dic[n]
下面这个会超时。。。
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1:
return 1
if n == 2:
return 2
return self.climbStairs(n-1) + self.climbStairs(n-2)
参考
class Solution(object):
def climbStairs(self, n):
a, b = 1, 1
for i in range(n):
a, b = b, a + b
return a
leetcode-easy-dynamic-70 Climbing Stairs的更多相关文章
- LeetCode练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
- [LeetCode&Python] Problem 70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【leetcode❤python】70. Climbing Stairs
#Method1:动态规划##当有n个台阶时,可供选择的走法可以分两类:###1,先跨一阶再跨完剩下n-1阶:###2,先跨2阶再跨完剩下n-2阶.###所以n阶的不同走法的数目是n-1阶和n-2阶的 ...
- Leetcode之70. Climbing Stairs Easy
Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...
- [LeetCode] 70. Climbing Stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...
- 42. leetcode 70. Climbing Stairs
70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- LN : leetcode 70 Climbing Stairs
lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...
- 刷题70. Climbing Stairs
一.题目说明 题目70. Climbing Stairs,爬台阶(楼梯),一次可以爬1.2个台阶,n层的台阶有几种爬法.难度是Easy! 二.我的解答 类似的题目做过,问题就变得非常简单.首先用递归方 ...
随机推荐
- 取出input内的空格
onkeyup='this.value=this.value.replace(' ','')'
- pidstat 命令(Linux 进程使用资源情况采样)
pidstat 作用 pidstat 获取服务器指定进程的使用资源信息(包括 CPU.设备IO.内存.线程.任务切换等). 执行一波 [root@wille ~]# pidstat Linux 2.6 ...
- shell知识点(二)
Shell 中的数组 Shell 数组用括号来表示,元素用"空格"符号分割开,语法格式如下: 方式2: arr=(value1 value2 value3) (这种方式带值) ...
- 爬虫相关基础技术铺垫---多线程Thread和队列Queue应用
from queue import Queue from threading import Thread class mydownloader(Thread): def __init__(self,q ...
- buuctf@reverse1
flag{hell0_w0rld}
- 【leetcode】341. Flatten Nested List Iterator
题目如下: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...
- EF Core命令
新建 Add-Migration init Update-Database init 修改model后,执行迁移的命令 更新数据库 每次更新都要{update}修改 Add-Migration {up ...
- node.js之CommonJS
1.CommonJS 我们只是在js文件中写下console.log('This is a test.');这句代码,node.js执行该js文件时: ( function(exports, requ ...
- C# 写日志的方法
public void WriteLog(string msg) { string filePath = AppDomain.CurrentDomain.BaseD ...
- C语言 - strcmp和strncmp的编程实现及总结
一.strcmp和strncmp的编程实现及总结 1.strcmp函数的实现 要求: 原型: int strcmp(char *dest,char * src,int n); 头文件:# ...