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 这个题目就是找到方程 f(i) = f(i - 1) + f(i - 2), i >= 2,然后用dynamic programming, 不过可以用滚动数组去优化。S:O(1) Code
class Solution:
def climbStairs(self, n):
mem = list(range(1, n + 1)) # in python3 , range is an iterator
for i in range(2, n):
mem[i] = mem[i - 1] + mem[i - 2]
return mem[n - 1]

滚动数组

class Solution:
def climbStairs(self, n):
mem = list(range(1, 4)) # in python3 , range is an iterator
for i in range(2, n):
mem[i%3] = mem[i%3 - 1] + mem[i%3 - 2]
return mem[(n - 1)%3]

滚动数组2

class Solution:
def climbStairs(self, n):
dp = [1, 2]
for i in range(2, n):
dp[i %2] = dp[(i - 1)%2] + dp[(i - 2)%2]
return dp[(n - 1)%2]
												

[LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming的更多相关文章

  1. 70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  2. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  3. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  4. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  5. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  7. [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  8. [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. [LeetCode] 198. House Robber _Easy tag: Dynamic Programming

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

随机推荐

  1. Oracle 升级的必要性

    一.Oracle 历史 Oracle database 作为Oracle 公司的商业产品,凭借其稳定性和运行高效占据了全球三成以上的市场.并且主要是金融.政府等领域. Oracle 数据库拥有近40年 ...

  2. python实现目录大小计算(含子目录)

    path=r"F:\\PYTHON\\day8\\ftp_server\\"#这里我直接写了固定地址,你可以自己输入 file_size = 0 def split_doc(pat ...

  3. SSL双向认证和SSL单向认证的流程和区别

    refs: SSL双向认证和SSL单向认证的区别https://www.jianshu.com/p/fb5fe0165ef2 图解 https 单向认证和双向认证!https://cloud.tenc ...

  4. nuxt

    nuxt nuxt 1.4.0 https://zh.nuxtjs.org/guide/installation vue init nuxt-community/starter-template te ...

  5. 企业级Docker-Harbor

    [环境准备] # yum install -y yum-utils device-mapper-persistent-data lvm2 下载docker-ce版本的yum源 # yum-config ...

  6. SpringBoot使用Nacos配置中心

    本文介绍SpringBoot如何使用阿里巴巴Nacos做配置中心. 1.Nacos简介 Nacos是阿里巴巴集团开源的一个易于使用的平台,专为动态服务发现,配置和服务管理而设计.它可以帮助您轻松构建云 ...

  7. ISP PIPLINE (附加1) Green Imbalance

    1.什么是Green imbalance 芯片的Gr和Gb通道获取的能量或者是输出的数据不一致,造成这种情况的原因一方面是Gr,Gb通道的半导体制造工艺方面的差异,另一方面是microlens的存在, ...

  8. 关于UTF-8和GBK编码的转换

    $oldname=mb_convert_encoding($_POST['oldname'], "GBK" , "UTF-8");//将变量转码为GBK,已知原 ...

  9. RHEL5.8安装

    创建完成后新的虚拟机.使用光盘启动后,启动界面如下图. 大概介绍下显示界面内容的意思:     1.To install or upgrade in graphical mde, press the ...

  10. Tips_一级菜单栏实现

    1.纵向 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...