【leetcode】 Unique Path ||(easy)
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.
思路:跟Unique Paths差不多,就是把有障碍的地方方法数变成0,注意左上角为障碍的情况
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(obstacleGrid.empty())
{
return ;
}
vector<vector<int>> ways(obstacleGrid.size(), vector<int>(obstacleGrid[].size(),));
if(obstacleGrid[][] == )
{
ways[][] = ;
}
for(int c = ; c < obstacleGrid[].size(); c++)
{
ways[][c] = (obstacleGrid[][c] == ) ? : ways[][c - ];
}
for(int r = ; r < obstacleGrid.size(); r++)
{
ways[r][] = (obstacleGrid[r][] == ) ? : ways[r - ][];
}
for(int i = ; i < obstacleGrid.size(); i++)
{
for(int j = ; j < obstacleGrid[].size(); j++)
{
ways[i][j] = (obstacleGrid[i][j] == ) ? : ways[i - ][j] + ways[i][j - ];
}
}
return ways[obstacleGrid.size() - ][obstacleGrid[].size() - ];
}
};
【leetcode】 Unique Path ||(easy)的更多相关文章
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【leetcode】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】Minimum Path Sum(easy)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【题解】【矩阵】【DP】【Leetcode】Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...
- 【LeetCode】437. Path Sum III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- 一行代码解决各种IE兼容问题IE8,IE9,IE10
一:一.指定文件兼容性模式(Xee:因为我已经放弃IE6,7了,所以以后设计的网页最低支持IE8;) 要为你的网页指定文件模式,需要在你的网页中使用meta元素放入X-UA-Compatible ht ...
- [译]AngularJS Service vs Factory - Once and for all
原文: http://blog.thoughtram.io/angular/2015/07/07/service-vs-factory-once-and-for-all.html Service和Fa ...
- 使用ajax获取JSON数据的jQuery代码的格式
具体的也可以参考:http://www.w3cfuns.com/notes/16039/2b004b1bcdf79092f2e66b2bbe9f51df.html
- 01WebApi防篡改机制---HMAC机制
防篡改,顾名思义就是防止有人恶意篡改请求数据URL以达到恶意攻击的目的,那要怎么才能实现这样的目的呢? 很简单,将要请求的数据加上合作号.合作Key按规则组织成一个字符串,获取对应的MD5摘要,然后将 ...
- 最简单的Android教程之自定义控件
新建title.xml,完成布局 新建一个TitleLayout继承 LinearLayout. activity_main.xml中引用 Run your applicaiton , and try ...
- 好无语的问题----include 后面需要空格么?
前俩天回学校办事,在去师弟宿舍的时候,被问到了一个很 "深奥"得问题 ---------include 后面需要空格么? 在我以前的印象中不管在哪个编译器中,,都是需要有空格的, ...
- caffe学习系列(2):训练和测试自己的图片
参考:http://www.cnblogs.com/denny402/p/5083300.html 上述主要介绍的是从自己的原始图片转为lmdb数据,再到训练.测试的整个流程(另外可参考薛开宇的笔记) ...
- PyCharm 入手第一记
因为我是忠实的Linux用户,所以一下操作是在Linux下的完成,除了下载,因为Linux的下载着实有点让人捉急. PyCharm 下载地址: http://www.jetbrains.com/pyc ...
- BZOJ 2541: [Ctsc2000]冰原探险
Descrption 有一些矩形障碍,碰到障碍会停下,求从一个点到另一个点的最少移动步数. Sol BFS. 因为题目的特殊性质,两个矩形没有任何相邻,起始点和终点和矩形没有相邻. 所以从一个点的移动 ...
- 13 HashTable抽象哈希表类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...