LeetCode63 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. (Medium)
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.
分析:
上一题的follow-up,加上了障碍物,思路一样,只是障碍物处dp[i][j] = 0;
代码:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
int dp[m][n];
memset(dp,,sizeof(dp));
for (int i = ; i < n; ++i) {
if (obstacleGrid[][i] == ) {
break;
}
dp[][i] = ;
}
for (int i = ; i < m; ++i) {
if (obstacleGrid[i][] == ) {
break;
}
dp[i][] = ;
}
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (obstacleGrid[i][j] == ) {
dp[i][j] = ;
}
else {
dp[i][j] = dp[i - ][j] + dp[i][j - ];
}
}
}
return dp[m - ][n - ];
}
};
注:为什么这道题里dp[m][n] = {0}不能把二维数组初始化为0了......
LeetCode63 Unique Paths II的更多相关文章
- leetcode-63. Unique Paths II · DP + vector
题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- Leetcode63.Unique Paths II不同路径2
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为" ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 61. Unique Paths && Unique Paths II
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
随机推荐
- 前端插件--isCroll的使用
中文文档地址: http://wiki.jikexueyuan.com/project/iscroll-5/ 效果图: <!DOCTYPE html> <html lang=&quo ...
- C# Action 和Func
https://www.cnblogs.com/LipeiNet/p/4694225.html
- springmvc java程序文件保存地址的路径问题
会保存为这种斜杠 不论之前填写的是什么样
- 如何在TypeScript中使用JS类库
使用流程 1.首先要清除类库是什么类型,不同的类库有不同的使用方式 2.寻找声明文件 JS类库一般有三类:全局类库.模块类库.UMD库.例如,jQuery是一种UMD库,既可以通过全局方式来引用,也可 ...
- flask的基本操作
常用的SQLAlchemy字段类型 # coding:utf-8 from flask import Flask from flask_sqlalchemy import SQLAlchemy app ...
- FreeMarker 对null值的处理技巧
以下引用官方描述: ? The FreeMarker template language doesn't know the Java language null at all. It doesn't ...
- springboot中logback打印日志(转)
springboot对logback的支持是非常好的,不需要任何配置,只需要在resource下加logback.xml就可以实现功能 直接贴代码: <?xml version="1. ...
- js顺序加载与并行加载
前端优化过程中常提到js的加载方式,下面说下几种常用的加载方式: 1:head标签内插入<script>标签 <script type="text/javaScript&q ...
- Unity 同一Text文本修改不同的字体大小和字体颜色
类似Html,在color和size对应的<>str</>中,就能修改str的相关属性, 下面的代码就是把time改为字体颜色为红色,大小为40: 而前面的"Time ...
- ubuntu16.04上在使用搜狗输入法时,按shift不能正常切换中英文
问题描述: ubuntu16.04上在使用搜狗输入法时,不知道把什么组合键给错按了,导致了按shift不能正常切换中英文,这是一件很烦恼的事儿! 解决步骤: 1,终端输入打开: fcitx-confi ...