LeetCode(63)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.
分析
带障碍的路径数计算问题,这个题目与上一题的区别之处在于,m*n的矩阵中有部分设置了障碍,当然有障碍的地方不能通过;
虽然设立了障碍,该题目的本质仍然是一个动态规划问题,我们只需要增加判断当前点是否有障碍的代码即可,若有障碍那么此处不能通行,自然f(i,j)=0,对于其他点,依然用上一题目的推导公式即可!
AC代码
//直接用非递归算法求解
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid.empty())
return 0;
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector<vector<int> > ret(m, vector<int>(n, 0));
//矩阵首列
for (int i = 0; i < m; i++)
{
//无障碍,则有一条路径,否则不通
if (obstacleGrid[i][0] != 1)
ret[i][0] = 1;
else
break;
}//for
//矩阵首行
for (int j = 0; j < n; j++)
{
//无障碍,则有一条路径,否则不通
if (obstacleGrid[0][j] != 1)
ret[0][j] = 1;
else
break;
}//for
//其余位置
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
//当前位置为障碍,则到此处路径数为0
if (obstacleGrid[i][j] == 1)
ret[i][j] = 0;
else{
ret[i][j] = ret[i][j - 1] + ret[i - 1][j];
}//else
}//for
}//for
return ret[m - 1][n - 1];
}//uniques
};
LeetCode(63)Unique Paths II的更多相关文章
- 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). ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(63):不同路径 II
Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(95) Unique Binary Search Trees II
题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
随机推荐
- -------Pokemon Master------很水-------
A - Pokemon Master Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- 进击的Python【第十章】:Python的高级应用(多进程,进程间通信,协程与异步,牛逼的IO多路复用)
Python的socket高级应用(多进程,协程与异步) 一.多进程multiprocessing multiprocessing is a package that supports spawnin ...
- [USACO 2012 Mar Silver] Landscaping【Edit Distance】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=126 好题啊好题,一开始就输给了这道题的想法! 先把原始状态以及目标状态换 ...
- 51nod 1116 K进制下的大数
你万万想不到,Long Long 就能存下的数据 #include <iostream> #include <cstdio> #include <cstdlib> ...
- 安装11g 数据库
出现问题解决: 1.首先确认下载的安装包完整性.2解压包的时候,按顺序解压,解压第一个包后,解压第二个包的时候,要把解压地址与解压第二包的地址要一样. 安装的时候,需要把两个压缩包都解压,并将目录wi ...
- 204 Count Primes 计数质数
计算所有小于非负整数 n 的质数数量. 详见:https://leetcode.com/problems/count-primes/description/ Java实现: 埃拉托斯特尼筛法:从2开始 ...
- APP增量更新
增量更新的概念: 当我们手机上安装的app版本与服务器的最新版本不一致的时候,传统做法是重新下载安装一个最新版的apk文件,不过这种方式比较耗流量,不利于用户体验.增量更新就是只下载当前app版本与最 ...
- 用jquery的.val() 给具有style="display:none;" 属性的标签写值的问题。
今天写项目, 碰到奇怪现象, 用jquery的val()函数怎么都无法给标签赋值,而我确定是否赋值是通过浏览器控制台来看的.其实这种方式不准确,因为具有 style="display:non ...
- 计算器Pro应用项目源码
本计算器实现了一些简单的功能,可能本身还存在一些缺陷,希望大家提建议,能够改进一下. 源码项目我已经上传到源码天堂那里了:http://code.662p.com/list/11_1.html < ...
- [转] 随机数是骗人的,.Net、Java、C为我作证
(转自:随机数是骗人的,.Net.Java.C为我作证 - 杨中科 原文日期:2014.05.12) 几乎所有编程语言中都提供了"生成一个随机数"的方法,也就是调用这个方法会生 ...