题目

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]就是需要的值

转载于:https://www.cnblogs.com/xbf9xbf/p/4250359.html

leetcode 【 Unique Paths 】python 实现相关推荐

  1. LeetCode Unique Paths

    原题链接在这里:https://leetcode.com/problems/unique-paths/ 题目: A robot is located at the top-left corner of ...

  2. LeetCode | Unique Paths I,II

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

  3. [leetcode]Unique Paths II

    >这道题目和上一题基本是一样的.只需要检测现在正在处理的那个单位是不是不可通过的,如果是就直接让这个单元格=0,也就是没有路径可以到右下角就好了. 代码如下: public class Solu ...

  4. [Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II

    115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Fo ...

  5. leetcode 62, 63, 980. Unique Paths I, II, III | 62, 63, 980. 不同路径 I, II, III(暴力递归->傻缓存->动态规划)

    62. Unique Paths https://leetcode.com/problems/unique-paths/ 注意本题只能向右 / 向上走. DP 问题,经典又熟悉. 暴力递归->傻 ...

  6. 【动态规划】LeetCode 63. Unique Paths II

    LeetCode 63. Unique Paths II Solution1:我的答案 在哪里做过这题? class Solution { public:int uniquePathsWithObst ...

  7. 【动态规划】LeetCode 62. Unique Paths

    LeetCode 62. Unique Paths Solution1:我的未能AC的答案 递归超时了!!! class Solution { public:int uniquePaths(int m ...

  8. LeetCode 1.Minimum Path Sum 2.Unique Paths I and II

    大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths A robot is located at the top-l ...

  9. Leetcode 62. Unique Paths

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

最新文章

  1. MacBookProM1 重置的注意事项
  2. 使用Atom编辑文本文件
  3. 2017年第1贴:EXT.JS使用MVC模式时,注意如何协调MODEL, STORE,VIEW,CONTROLLER的关系
  4. php搜索文件名,window_Windows7内置搜索如何同时搜索文件名与内容,  Win7的搜索功能效果非常强 - phpStudy...
  5. 前端常用插件、工具类库汇总,新手必收藏!!!
  6. sap系统搭建教程_手把手安装SAP ERP系统
  7. 计算机二级在线练,计算机二级操作练习题.doc
  8. 【软件介绍】GWAS meta分析软件:METAL
  9. 艾孜尔江_国二MS Office考试Excel函数常考知识点
  10. Linux系统分区概念
  11. 选第二大算法(锦标赛算法)
  12. android 访问data目录、6.0模拟器读写sdcard、相关sdcard路径
  13. android环信聊天界面上面显示昵称,【环信征文】在android中5分钟实现环信昵称头像的显示...
  14. 透明质酸/氧化石墨烯/聚丙烯酰胺复合水凝胶/透明质酸/βTCP复合水凝胶研究制备
  15. i386 Linux内核进入保护模式引导流程
  16. iPhone连接电脑WIN7不显示Apple iPhone便携式设备的解决办法
  17. Delete `␍`eslint(prettier/prettier) 错误的解决方案
  18. 自己动手写word2vec (四):CBOW和skip-gram模型
  19. tensorflow基础知识(CNN)
  20. 基于MPPT算法的PV光伏阵列电网模型simulink仿真

热门文章

  1. 展望我的2022Flag
  2. 枚举法 之Java实现凑硬币
  3. 室内空气流动原理图_家庭新风系统示意图 新风系统运行原理介绍
  4. 重磅!GitHub 开源负载均衡组件 GLB Director
  5. Redis基础知识之—— hset 和hsetnx 的区别
  6. 如何通过插件携带第二个单据体到下游单据
  7. Advanced Linq - Dynamic Linq query library: Add support for 'Contains' extension
  8. C语言常见单链表面试题(2)
  9. 技术人员应该如何培养
  10. 脚印:关于错误编码的管理的一些思考