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?

Note: m and n will be at most 100.

Example 1:

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right

Example 2:

Input: m = 7, n = 3
Output: 28 思路

  在求路径数这一方面的问题时,第一想到的就是回溯法求解。因为每次只能左移动和下移动,因此移动之后判断以下是否超出运动范围,然后再继续向左或向下移动。使用一个变量来记录路径数。
  使用回溯法写完之后发现运行时间超时了,那就说明回溯法不是这道题的最优解,因此我们可以使用动态规划来解决,我们申请一个辅助矩阵,然后值都初始化为1, 行和列都是从1开始开始计数,动态方程为dp[i][j] = dp[i-1][j] + dp[i][j-1] 。一直到矩阵的最后一个元素。而该元素的值就是结果数。
  另外因此根据这个动态方程我们可以发现每次只用到了上一行同位置的元素和同行中前一个位置的元素。根据这个特点我们可以设置一个长度和列长相等的辅助数据,然后动态方程为dp[j] += dp[j-1]。循环row次,最终数组中最后一个元素的值就为结果。时间复杂度为O(row*cloum), 空间复杂度为O(cloum)。
动态规划的图示步骤

解决代码

    回溯法
 class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
""" count = [0] # 记录结果数
self.Backtracking(m-1, n-1, 0, 0, count)
return count[0] # 返回结果 def Backtracking(self, m, n,row, cloum, count): # 回溯函数
if row == m and cloum == n: # 达到右下角,增加数量
count[0] += 1
return
if row > m or cloum > n: # 越界直接返回
return
if row != m: # 等于m的时候不能向下走
self.Backtracking(m, n, row+1, cloum, count)
if cloum != n: # 等于n的时候不能向右走。
self.Backtracking(m, n, row, cloum+1, count)

  动态规划(第一种办法)

 class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
dp = []
for i in range(m): # 申请一个m*n的矩阵,并将值初始化为1
dp.append([1]*n) for i in range(1, m): # 从1开始遍历,得到每一个位置的路径树
for j in range(1, n):
dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[m-1][n-1] # 最终结果

  动态规划第二种思路

 class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
""" dp = [1]*n # 申请长度为n的数组
for i in range(1, m): # 遍历m行,每行n个位置
for j in range(1,n):
dp[j] += dp[j-1] return dp[-1] 返回结果

【LeetCode每天一题】Unique Paths(唯一的路径数)的更多相关文章

  1. 【js】Leetcode每日一题-停在原地的方案数

    [js]Leetcode每日一题-停在原地的方案数 [题目描述] 有一个长度为 arrLen 的数组,开始有一个指针在索引 0 处. 每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指 ...

  2. [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). The ...

  3. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

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

  4. [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 ...

  5. 【leetcode】62.63 Unique Paths

    62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the di ...

  6. [Leetcode] unique paths ii 独特路径

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

  7. LeetCode(63)Unique Paths II

    题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...

  8. [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). The ...

  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. 10分钟理解JS引擎的执行机制

    首先,请牢记2点: (1) JS是单线程语言 (2) JS的Event Loop是JS的执行机制.深入了解JS的执行,就等于深入了解JS里的event loop 1.灵魂三问 (1) JS为什么是单线 ...

  2. 解决爬虫中遇到的js加密问题之有道登录js逆向解析

    具体实现在github上面(有详细的步骤): https://github.com/WYL-BruceLong/Spider_JS_ReverseParsin

  3. 微信小程序换皮肤,动态切换菜单栏和导航栏的样式,动态修改TabBar和NavigationBar

    在做微信小程序换皮肤的时候,需要动态修改菜单栏(TabBar)和导航栏(NavigationBar) 但是在小程序中它们的样式是写在app.json里面,而且app.json是静态编译,运行时哪怕你修 ...

  4. 快速启动FTP下载服务器

    nohup python -m SimpleHTTPServer 8000  > myout.file 2>&1 &

  5. Fiddler 抓包设置

    手机抓包设置 一:配置Fiddler参数 打开Fiddler菜单项Tools->TelerikFiddler Options->HTTPS, 勾选CaptureHTTPS CONNECTs ...

  6. Notes for "Python in a Nutshell"

    Introduction to Python Wrap C/C++ libraries into Python via Cython and CFFI. Python implementations ...

  7. Linux 包管理

    1 文集 <Linux 包管理基础:apt.yum.dnf 和 pkg>,由Snapcrafter翻译,英文原创(作者Brennen)地址在这里. 这是一篇不错的概括性的文章,系统介绍了D ...

  8. Eclipse中如何将项目转成Dynamic Web项目

    项目接手前的同事是使用MyEclipse写的,拿SVN将项目导入eclipse时,web项目就会变成java项目,这时就需要修改配置文件将项目转成web,步骤如下: 1.修改项目配置文件. 要修改的项 ...

  9. leetcode刷题第二天<两数相加>

    题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  10. Centos6.5部署vsftpd+mysql认证

    1.FTP传输原理 FTP,文件传输协议,是工作在应用层,基于TCP实现,依赖于互联网即可通讯. 1)连接模式 控制(命令)连接,用来通信,一直在线,客户端随机端口连接服务端TCP:21端口. 数据连 ...