【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 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(唯一的路径数)的更多相关文章
- 【js】Leetcode每日一题-停在原地的方案数
[js]Leetcode每日一题-停在原地的方案数 [题目描述] 有一个长度为 arrLen 的数组,开始有一个指针在索引 0 处. 每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指 ...
- [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 ...
- 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). ...
- [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 ...
- 【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 ...
- [Leetcode] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode(63)Unique Paths II
题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...
- [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 ...
- 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). ...
随机推荐
- 末学者笔记--Linux计划任务及压缩归档
一.计划任务 1.介绍: (1)定义:简单说就是通过一些设置,来使linux系统定时执行一些操作与任务. (2)作用:一般可执行一些周期性操作,也可定期备份数据. (3)可使用的命令:常用为at和cr ...
- 使用kubeadm创建kubernets集群
参考: http://docs.kubernetes.org.cn/459.html https://blog.csdn.net/gui951753/article/details/833169 ...
- mysql 删除所有表
SELECT concat('DROP TABLE IF EXISTS ', table_name, ';')FROM information_schema.tablesWHERE table_sch ...
- stm32位操作详解
stm32位操作详解 STM32位操作原理 思想:把一个比特分成32位,每位都分配一个地址,这样就有32个地址,通过地址直接访问. 位操作基础 位运算 位运算的运算分量只能是整型或字符型数据,位运算把 ...
- Selenium2+python自动化45-18种定位方法(find_elements)
前言 江湖传言,武林中流传八种定位,其中xpath是宝刀屠龙,css是倚天剑. 除了这八种,其实还有十种定位方法,眼看就快失传了,今天小编让失传已久的定位方法重出江湖! 一.十八种定位方法 前八种是大 ...
- ef core的外键约束笔记
ef core设置可选外键,有如下几种方式:1.在依赖实体AAA中,并不显式设置外键属性XXXId 2.手动设置外键属性XXXId为可空类型(int?等类型) 3.在实体类与数据表进行映射时,配置狭隘 ...
- Linux安装RocketMQ
本文介绍Linux安装RocketMQ. 1.RocketMQ简介 RocketMQ是阿里巴巴中间件开发的分布式消息系统,曾经经历过很多阿里巴巴大型项目的实际检验.在去年已经正式捐献给Apache开源 ...
- GUI Design Studio的使用方法
一.GUI Design Studio的介绍 GUI DesignStudio 是一个给应用软件设计图形用户界面的专业工具,它可在画基于web形态的原型时,可以用 Axure RP. Balsamiq ...
- Intellij IDEA导入eclipse项目配置jdk、tomcat到浏览器正常访问
转发自:博客园---Lindp(大佬写的甚好) 以下是转发的正文 intellij idea中文资料网上比较少,对于eclipse的项目如何导入intellij idea也没有完整的说明,本人在这里整 ...
- Python requests--初识接口自动化
requests模块初级宝典:http://docs.python-requests.org/zh_CN/latest/user/quickstart.htmlrequests模块之葵花宝典:http ...