[Leetcode] 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 as1and0respectively 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 is2.
Note: m and n will be at most 100.
题意:增加障碍,不能到达障碍,不能越过障碍。
思路:思路和unique paths是一样的,只是要判断当前值是否为1,若是,则其对应的dp数组中赋值为0,不是时,状态方程是:dp[i][j]=dp[i-1][j]+dp[i][j-1]。代码如下:
- class Solution {
- public:
- int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid)
- {
- int m=obstacleGrid.size(),n=obstacleGrid[].size();
- vector<vector<int>> dp(m,vector<int>(n,));
- if(m==||n==) return ;
- if(obstacleGrid[][]==) return ;
- dp[][]=; //初始行
- for(int i=;i<m;++i)
- {
- if(obstacleGrid[i][] ==)
- {
- break;
- }
- else
- dp[i][]=;
- }
- //初始列
- for(int i=;i<n;++i)
- {
- if(obstacleGrid[][i] ==)
- {
- break;
- }
- else
- dp[][i]=;
- }
- for(int i=;i<m;++i)
- {
- for(int j=;j<n;++j)
- {
- if(obstacleGrid[i][j] !=)
- dp[i][j]=dp[i-][j]+dp[i][j-];
- }
- }
- return dp[m-][n-];
- }
- };
当用一维数组去简化时,要注意些问题,仅一行,或者仅一列时,还要依次的确定数组dp[]中对应的值,不是像上一题那样简单赋值为1就行,所以,遍历时,行和列的起始点都是从0开始;另外也得注意的是,数组dp中的当前的前一个是否存在,即, j >0。代码如下:
- class Solution {
- public:
- int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid)
- {
- int row=obstacleGrid.size(),col=obstacleGrid[].size();
- if(obstacleGrid[][]==) return ;
- vector<int> dp(col,);
- dp[]=;
- for(int i=;i<row;++i)
- {
- for(int j=;j<col;++j)
- {
- if(obstacleGrid[i][j]==)
- dp[j]=;
- else if(j>)
- dp[j]+=dp[j-];
- }
- }
- return dp[col-];
- }
- };
[Leetcode] unique paths ii 独特路径的更多相关文章
- [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 [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- [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不同路径 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 II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 063 Unique Paths II 不同路径 II
这是“不同路径” 的进阶问题:现在考虑网格中有障碍物.那样将会有多少条不同的路径从左上角到右下角?网格中的障碍物和空位置分别用 1 和 0 来表示.例如,如下所示在 3x3 的网格中有一个障碍物.[ ...
随机推荐
- HTML自定义Checkbox框背景色
input[type=checkbox]{ margin-right:5px; width:13px; height:13px; }input[type=checkbox]:after { width ...
- Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000
启动程序报错: Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000006fff80000, 28636 ...
- python线程与进程小结
传统方式是调用2个方法执行1个任务,方法按顺序依次执行 # -*- coding:utf-8 -*- import threading import time def run(n): print('t ...
- Python学习手册之函数和模块
在上一篇文章中,我们介绍了 Python 的控制结构,现在我们介绍 Python 函数和模块. 查看上一篇文章请点击:https://www.cnblogs.com/dustman/p/9976234 ...
- Go语言使用百度翻译api
Go语言使用百度翻译api 之前做过一个使用百度翻译api的工具,这个工具用于用户的自动翻译功能,是使用C#调用百度翻译api接口,既然在学习Go语言,那必然也是要使用Go来玩耍一番.这里我是这么安排 ...
- 46-Identity MVC:登录逻辑实现
1- Login.cshtml <h3>Login</h3> @model MvcCookieAuthSample.ViewModel.LoginViewModel <d ...
- js分类多选全选
效果如图: HTML代码: <div class="form-group quanxian-wrap"> <label>项目</label> & ...
- 生产者与消费者-N:1-基于list
多个生产者/一个消费者: /** * 生产者 */ public class P { private MyStack stack; public P(MyStack stack) { this.sta ...
- CSS3 : transition 属性
CSS3的 transition 属性用于状态过度效果! 1.语法: transition: property duration timing-function delay; -moz-transit ...
- 问题 C: Goldbach's Conjecture
题目描述 Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least ...