题目

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

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

代码:oj测试通过 Runtime: 44 ms

  1. class Solution:
  2. # @return an integer
  3. def uniquePaths(self, m, n):
  4. # none case
  5. if m < 1 or n < 1:
  6. return 0
  7. # special case
  8. if m==1 or n==1 :
  9. return 1
  10.  
  11. # dp
  12. dp = [[0 for col in range(n)] for row in range(m)]
  13. # the elements in frist row have only one avaialbe pre-node
  14. for i in range(n):
  15. dp[0][i]=1
  16. # the elements in first column have only one avaialble pre-node
  17. for i in range(m):
  18. dp[i][0]=1
  19. # iterator other elements in the 2D-matrix
  20. for row in range(1,m):
  21. for col in range(1,n):
  22. dp[row][col]=dp[row-1][col]+dp[row][col-1]
  23.  
  24. return dp[m-1][n-1]

思路

动态规划经典题目,用迭代的方法解决。

1. 先处理none case和special case

2. 2D-matrix的第一行和第一列上的元素 只能从上面的元素或左边的元素达到,因此可以直接获得其值

3. 遍历其余的位置:每一个position只能由其左边或者上边的元素达到,这样可得迭代公式 dp[row][col]=dp[row-1][col]+dp[row][col-1]

4. 遍历完成后 dp矩阵存放了从其实位置到当前位置的所有可能走法,因此返回dp[m-1][n-1]就是需要的值

leetcode 【 Unique Paths 】python 实现的更多相关文章

  1. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  2. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  3. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  4. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  5. [LeetCode] Unique Paths 不同的路径

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

  6. LeetCode: Unique Paths I & II & Minimum Path Sum

    Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m  ...

  7. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  8. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  10. Leetcode Unique Paths

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

随机推荐

  1. 【Mood-13】Android --如何从初级工程师进化为高级工程师

    一  明确自我定位 现在你是初级工程师,但是你想当个高级工程师,所 以,你就要给自己定个目标,即:我是要成为高级工程师的男人.有了这个定位,并且努力朝着这个目标去努力,然后内心深处就会有一个感觉,这个 ...

  2. Mysql数据库操作语句总结(三)

    最近一段时间重新学习一下mysql命令行的用法, 这里简单记录一下 参考文章: https://www.cnblogs.com/bluealine/p/7832219.html 个人使用的是mysql ...

  3. Cloneable接口的作用

    Cloneable接口是一个[标记接口],就是没有任何内容 implements Cloneable表示该对象能被克隆,能使用Object.clone()方法.如果没有implements Clone ...

  4. uLua学习之创建游戏对象(二)

    前言 上节,刚刚说到创建一个“HelloWorld”程序,大家想必都对uLua有所了解了,现在我们一步步地深入学习.在有关uLua的介绍中(在这里),我们可以发现它使用的框架是Lua + LuaJIT ...

  5. 微软AI发布会,集齐六大亮点召唤黑科技!

    7月12日,微软合作伙伴大会Inspire在华盛顿特区如火如荼地举行.同一天,在相隔5个时区的伦敦,微软还举办了一场关于人工智能的发布会.这是一场智能技术和情感体验两相交融的科技盛宴,既有黑科技,也有 ...

  6. World Wind Java开发之十三——加载Geoserver发布的WMS服务(转)

    这篇是转载的平常心博客,原地址见:http://www.v5cn.cn/?p=171 1.WMSTiledImageLayer类说明 一个WMSTiledImageLayer类对象只能对应一个WMS发 ...

  7. Linux系统运维常见面试简答题(36题)

    1.请描述下linux 系统的开机启动过程开机加电BIOS自检———–>MBR引导———–>grub引导菜单———–>加载内核———–>启动init进程———–>读取in ...

  8. 输出流缓冲的意义 何时缓冲 Stdout Buffering

    From : https://eklitzke.org/stdout-buffering 译者:李秋豪 大多数编程语言默认提供了i/o缓冲特性,因为这会使得输出更加有效率.这些缓冲功能大都是默默工作& ...

  9. SQL查询出每门课都大于80 分的学生姓名

    Course表如下: 查询出每门课都大于80 分的学生姓名有两种方法. 1.select  distinct name from Course where name not in (select di ...

  10. centos 6 安装VMware Tools

    开启虚拟机的centos系统, 在虚拟机工具栏点击 “虚拟机”=>VMwareTools安装,  centos系统内的桌面会有一个VMware Tools的驱动光驱, 双击打开后,有一个tar. ...