题目简述:

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?

解题思路:

这个是最简单的动态规划问题,我们考虑爬上n阶有几种情况?既然只能一次走一步或者是两步,那么要到第n阶的种数要不然是从第n-1阶走一步上去,要不然是从第n-2阶走两步上去。写成数学式子就是:\(a[n]=a[n-1]+a[n-2]\)

class Solution:
# @param n, an integer
# @return an integer
def climbStairs(self, n):
if n == 1:
return 1
if n == 2:
return 2
a = [None] * n
a[0] = 1
a[1] = 2
for i in range(2,n):
a[i] = a[i-1] + a[i-2]
return a[n-1]

【leetcode】Climbing Stairs的更多相关文章

  1. 【leetcode】Climbing Stairs (easy)

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

  2. 【题解】【DP】【Leetcode】Climbing Stairs

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

  3. 【Leetcode】【Easy】Climbing Stairs

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

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. Python 从零学起(纯基础) 笔记 之 collection系列

    Collection系列  1.  计数器(Counter) Counter是对字典类型的补充,用于追踪值的出现次数   ps  具备字典所有功能 + 自己的功能 Counter import col ...

  2. Map工具系列-04-SQL合并执行工具

    所有cs端工具集成了一个工具面板 -打开(IE) Map工具系列-01-Map代码生成工具说明 Map工具系列-02-数据迁移工具使用说明 Map工具系列-03-代码生成BySQl工具使用说明 Map ...

  3. MySQL基本命令

    1.修改root用户登录密码: [root@17track bin]# /usr/local/mysql/bin/mysqladmin -u root password 'MyPassword' my ...

  4. 触发器运用示例---laobai

    1 触发器 概念:trigger.逻辑对象的一种.当dml的增删改语句执行时,自动触发一系列动作. 分类:dml触发器.ddl触发器(很少见) sql:ddl,dml,dcl 按触发的时间分: 语句执 ...

  5. ngInclude与script加载模板

    ng-include: 官网实例: <p>ng-include:</p> <select ng-model="template" ng-options ...

  6. centos apache svn配置

    单独安装svn服务: 安装svn软件 yum install subversion 创建根目录 mkdir -p /var/www/svn 创建版本库repos svnadmin create /va ...

  7. ajax方法简单实现

    //option {url,medthod,type,data,fSuccess,fError} function ajax(option) { var xhr = window.XMLHttpRqu ...

  8. Protecting against XML Entity Expansion attacks

    https://blogs.msdn.microsoft.com/tomholl/2009/05/21/protecting-against-xml-entity-expansion-attacks/ ...

  9. Bash 为何要发明 shopt 命令

    在 Bash 中,有两个内置命令用来控制 Bash 的各种可配置行为的开关(打开或关闭),这些开关称之为选项(option).其中一个命令是 set,set 命令有三种功能:显示所有的变量和函数:修改 ...

  10. cpu对各种运算的速度对比

    今天忽然想到cpu对各种基本运算的速度对比,我对比了异或,加法和乘法,结果发现速度时间,加法小于乘法小于异或, 原本我以为异或会是最快的,结果异或是最慢的,这跟cpu中的alu算术逻辑单元的实现有关, ...