题目

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

 class Solution:
# @return an integer
def uniquePaths(self, m, n):
# none case
if m < 1 or n < 1:
return 0
# special case
if m==1 or n==1 :
return 1 # dp
dp = [[0 for col in range(n)] for row in range(m)]
# the elements in frist row have only one avaialbe pre-node
for i in range(n):
dp[0][i]=1
# the elements in first column have only one avaialble pre-node
for i in range(m):
dp[i][0]=1
# iterator other elements in the 2D-matrix
for row in range(1,m):
for col in range(1,n):
dp[row][col]=dp[row-1][col]+dp[row][col-1] 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. 【MFC】获取文件大小的方法

    [转载]原文地址:http://blog.csdn.net/coderwu/article/details/5652056 MFC 下可以通过 CFileStatus 获取文件大小. ULONGLON ...

  2. Spring Boot : Swagger 2

    每次修改完代码需要找原本的API时楼主的内心是痛苦的,因为一般情况下都找不到,需要重新写一份.如果使用Swagger的话,只要加几个注解就可以实时生成最新的在线API文档,而且不仅仅是文档,同时支持A ...

  3. SQLSERVER编译与重编译

    SQLSERVER编译与重编译 编译的含义 当SQLSERVER收到任何一个指令,包括查询(query).批处理(batch).存储过程.触发器(trigger) .预编译指令(prepared st ...

  4. LeetCode Move Zeroes (简单题)

    题意: 给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变. 思路: 扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了. C++ class Solu ...

  5. World Wind Java开发之七——读取本地栅格文件(影像+高程)构建三维场景(转)

    http://blog.csdn.net/giser_whu/article/details/41679515 首先,看下本篇博客要达到的效果图: 下面逐步分析如何加载影像及高程文件. 1.World ...

  6. PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)

    http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...

  7. C#语句对Access中数据更新问题――Update语法错误

    所有字段最好都用[] 括起来: string sqlUpdate = "update UserInfo set [password] = '" + pass + "',[ ...

  8. ASP.NET 与 Ajax 的实现方式

    Ajax 应该不是一项技术,是一种思想而已,跟 ASP.NET 以及其它 Web 开发语言没有什么太大关系,这里只是谈谈 ASP.NET 中目前使用的 Ajax 技术以及其它一些实现 Ajax 的优秀 ...

  9. 导航条(Navbar)

    1.添加.navbar-fixed-top类可以让导航条固定的页面的顶部,固定的导航条会遮住页面上其它的内容,除非给body元素设置padding,导航条默认高度为50px ,因此可以给body元素设 ...

  10. C# 创建子目录

    运用DirectoryInfo类创建子目录是非常容易的,你只要调用其中CreateSubdirectory()方法即可,演示代码如下. DirectoryInfo dir = new Director ...