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.
思路: 其实答案就是 C(m+n-2, m-1). 但是写程序利用动态规划会简单快捷。(给两个代码,第一个方便理解,第二个是基于第一个的优化)
1.
class Solution { // C(m+n-2, m-1)
public:
int uniquePaths(int m, int n) {
vector<vector<int> > times(m, vector<int>(n, 0));
for(int r = 0; r < m; ++r) times[r][0] = 1;
for(int c = 1; c < n; ++c) times[0][c] = 1; // 只能到 1 次
for(int r = 1; r < m; ++r)
for(int c = 1; c < n; ++c)
times[r][c] = times[r-1][c] + times[r][c-1];
return times[m-1][n-1];
}
};
2.
class Solution { // C(m+n-2, m-1)
public:
int uniquePaths(int m, int n) {
if(m <= 0 || n <= 0) return 0;
vector<int> R(n, 1); // 一行行的记录
for(int r = 1; r < m; ++r)
for(int c = 1; c < n; ++c)
R[c] = R[c]+ R[c-1];
return R[n-1];
}
};
Unique Paths II
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1
and 0
respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2
.
Note: m and n will be at most 100.
思路:同上,只是最初初始化全 0 . 当前位置为 1 时,则当到达前位置的步数为 0.
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(!obstacleGrid.size() || !obstacleGrid[0].size()) return 0;
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
vector<int> R(n, 0);
R[0] = 1-obstacleGrid[0][0];
for(int r = 0; r < m; ++r)
for(int c = 0; c < n; ++c) {
if(c > 0)
R[c] = (obstacleGrid[r][c] == 1 ? 0 : (R[c] + R[c-1]));
else if(obstacleGrid[r][c] == 1) R[0] = 0;
}
return R[n-1];
}
};
61. Unique Paths && Unique Paths II的更多相关文章
-
【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
-
【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
-
41. Unique Binary Search Trees &;&; Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
-
LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
-
Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
-
[LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
-
LeetCode解题报告—— Reverse Linked List II &; Restore IP Addresses &; Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
-
leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
-
【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
随机推荐
-
rhel7 单用户修改root密码
rhel7密码忘记的时候,可以通过单用户模式修改密码 1.修改 引导文件,添加rw init=/sysroot/bin/sh ,ctrl+x启动 2.切换根chroot /sysroot3.使用pas ...
-
objective-c字符串笔记
字符串 // 字符串 分可变字符串和不可变字符串 // 不可变字符串的初始化方式 // NSString *string = [[NSString allo ...
-
f2fs源码解析(五) node管理结构梳理
node是f2fs重要的管理结构, 它非常重要! 系统挂载完毕后, 会有一个f2fs_nm_info结构的node管理器来管理node的分配. f2fs_nm_info中最让人疑惑的是几颗基数树: s ...
-
标题右边10px位置紧跟发布时间
一个ul列表,拥有若干li,内容是新闻标题,标题右边10px位置紧跟发布时间,当标题过长需要控制标题width,需要兼容ie6,不能用max-width h4{font-size:14px;heigh ...
-
JAVA生成验证码
<img border="0" src="ValidateCode" ...
-
IIS服务器环境配置(一)
在开始-> 控制面板 ->打开或关闭功能 IIS 服务器程序 勾选 HTML勾选 完成添加后 重启 确认是否添加上在控制面板的 管理工具中查看
-
saiku中文维度,补充说明
saiku在筛选中文维度 会出现浏览器白屏 停止响应的现象,经过跟踪源代码,分析原来在linux 操作系统中 数据库读取的中文和界面选取的编码是不一致的 解决方法, classes\saiku-dat ...
-
lambda表达式——写多线程
JDK1.8 中Lambda 表达式的出现,基本可以取替原来的匿名类实现多线程的方式.下面列举常用的常用的三种情况. 一.普通开启异步线程 new Thread(() -> System.o ...
-
H5 五子棋源码
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
-
三国群英传2修改MOD基础
三国群英传2的MOD制作,必须修改的几个ini文件: SANGO.INI--武将的武器.马匹.物品 THINGS.INI--战场中的对象:兵种.兵种在战场的设定.武器等 TIMES1-4.INI--剧 ...