【Leetcode】【Medium】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?

Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
思路1:
一上去就觉得是简单题:
如果当前格位于第m行或第n列,则只有一种路径;
否则当前格路径数等于“右格路径数”+“下格路径数”;
代码(未AC):
class Solution {
public:
int uniquePaths(int m, int n) {
if (m == || n == )
return ;
return uniquePaths(m - , n) + uniquePaths(m, n - );
}
};
结果提示超时了。
思路2:
考虑超时原因很可能是使用了函数递归,为避免使用递归,新建一个m*n的矩阵空间用于保存每个点计算的路径数。用新建空间保存结果,代替递归。
代码(AC):
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > grid(m, vector<int>(n, ));
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
grid[i][j] = grid[i-][j] + grid[i][j-];
}
}
return grid[m-][n-];
}
};
思路3:
上述代码时间复杂度o(m*n),空间复杂度o(m*n),通过观察路径数量规律,还可以减少空间复杂度为o(n)。
已知grid[i][j] = grid[i-1][j] + grid[i][j-1];
进一步将后一项grid[i][j-1]替换为grid[i-1][j-1] + grid[i][j-2];
不断查分后一项,最终grid[i][j] = grid[i-1][j] + grid[i-1][j-1] + grid[i-1][j-2] + ... + grid[i-1][1] + grid[i][0];
又因为grid[i][0] = grid[i-1][0] = 1;
所以grid[i][j] 就等于第i-1行,从0到j所有元素之和;
得到了这个规律,我们只需要一个长度为n的数组col,通过第0行计算第1行,并不断迭代,最终得到第m行格子存在的路径数,此时col[n-1]即为所求.
class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> col(n, );
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
col[j] = col[j-] + col[j];
}
}
return col[n-];
}
};
思路4:
可以通过分析排列组合暴力求解:
从格子起始,一共需要移动n+m-2步,可以到达终点。
这n+m-2步中,有m-1步需要向下移动。
问题转化为,从n+m-2步中,选择m-1步向下移动,有多少种选择方法。
因此通过计算Combination(n+m-2, m-1)即可求得答案.
代码(超时):
class Solution {
public:
int uniquePaths(int m, int n) {
long long dividend = ;
long long divisor = ;
for (int i = ; i <= m - ; ++i) {
dividend *= i + n - ;
divisor *= i;
}
return int(dividend / divisor);
}
};
代码超时,未AC,正要放弃,看了讨论区的代码..原来用浮点数直接除,结果是正确的;
即(代码AC):
class Solution {
public:
int uniquePaths(int m, int n) {
double res = ;
for (int i = ; i <= m - ; ++i) {
res = res * (i + n - ) / i;
}
return int(res);
}
};
【Leetcode】【Medium】Unique Paths的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【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刷题笔记】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- <LeetCode OJ> 62. / 63. Unique Paths(I / II)
62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- 【leetcode刷题笔记】Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【leetcode刷题笔记】Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- 2019.04.19 读书笔记 比较File.OpenRead()和File.ReadAllBytes()的差异
最近涉及到流的获取与转化,终于要还流的债了. 百度了一下,看到这样的两条回复,于是好奇心,决定看看两种写法的源码差异. 先来看看OpenRead() public static FileStream ...
- c# xml序列化和反序列化。也就是xml的解析和反解析。
用习惯了newTownSoft.json 的json反序列化.碰到xml是真的不习惯. 每次json反序列化都是直接把json丢到bejson网站生成一个实体类,稍微修改修改一点点变量名.然后直接ne ...
- SuperMap iClient for JavaScript 之关联查询
人们常说,计划赶不上变化.同样的,在项目中,使用的数据也是在不断变化的,尤其是属性信息的改变.就比如说,地图上的地物,它的空间信息在比较长的时间内,都不会发生变化,他的属性信息在初期不完整或者与后来的 ...
- elasticsearch安装及与springboot2.x整合
关于elasticsearch是什么.elasticsearch的原理及elasticsearch能干什么,就不多说了,主要记录下自己的一个使用过程. 1.安装 elasticsearch是用java ...
- RequireJs学习笔记之Define a Module
简单的键值对定义define({ color: "black", size: "unisize"}); 如果一个模块没有任何依赖,又需要做用一个函数 ...
- Freemarker list的使用
更新多条记录的操作,这里ids是一个数组 <sqltemplate id = "disableBuildLabourer"> <![CDATA[ UPDATE b ...
- 从零实现一个简易的jQuery框架之二—核心思路详解
如何读源码 jQuery整体框架甚是复杂,也不易读懂.但是若想要在前端的路上走得更远.更好,研究分析前端的框架无疑是进阶路上必经之路.但是庞大的源码往往让我们不知道从何处开始下手.在很长的时间里我也被 ...
- bzoj 5372: [Pkusc2018]神仙的游戏
Description 小D和小H是两位神仙.他们经常在一起玩神仙才会玩的一些游戏,比如"口算一个4位数是不是完全平方数". 今天他们发现了一种新的游戏:首先称s长度为len的前缀 ...
- 【idea--git】
http://blog.csdn.net/autfish/article/details/52513465 工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张, ...
- docker 开机自动启动容器
注意:如果有存在多个容器,都占用了同一端口,那么只会起来一个,要注意,我在调试时候就遇到这个坑了 在使用docker run启动容器时,使用--restart参数来设置: docker run -m ...