Description:

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.

分析:这道题目跟之前的UniquePath基本都是一样的,只是在格子中加了障碍物。基本思路是深搜+备忘录

但是不知道为什么会超时。。基本就跟答案一样,以后分析

 int pathrec[][]={};
class Solution {
public: int findpath(vector<vector<int> >& grid, int m, int n,int x,int y)
{
if(grid[m][n]==)
return ; if(pathrec[m][n]!=)
return pathrec[m][n];
int pathnum = ;
if(m==x- && n==y-)
{
pathrec[m][n] = ;
return ;
} if(m+<x) pathnum+= findpath(grid,m+,n,x,y);
if(n+<y) pathnum+= findpath(grid,m,n+,x,y);
pathrec[m][n] = pathnum;
return pathnum;
}
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(obstacleGrid.empty() || obstacleGrid[].empty()) return ;
int x = obstacleGrid.size(); int y= obstacleGrid[].size();
memset(pathrec,,sizeof(pathrec));
int pathnum = findpath(obstacleGrid,,,x,y);
return pathnum;
}
};
 

Leetcode: UniquePath II的更多相关文章

  1. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  2. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. LeetCode Permutaions II

    LeetCode解题之Permutaions II 原题 输出一个有反复数字的数组的全排列. 注意点: 反复数字的可能导致反复的排列 样例: 输入: nums = [1, 2, 1] 输出: [[1, ...

  4. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  5. [LeetCode] H-Index II 求H指数之二

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

  6. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  7. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  8. LeetCode H-Index II

    原题链接在这里:https://leetcode.com/problems/h-index-ii/ 题目: Follow up for H-Index: What if the citations a ...

  9. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

随机推荐

  1. .NET 对象序列化和系列化德

    DataSet ds = new DataSet(); //给ds赋值(省略) byte[] b = this.Serialize(ds); DataSet d1 = this.DeSerialize ...

  2. GG同步sqlserver报错一个案例 Invalid date format

    在里面Oracle表同步sqlserver时间,在sqlserver当应用程序数据的结束.您可能会遇到这个错误. 2014-05-17 17:20:24 WARNING OGG-01154 SQL e ...

  3. SQL ROW_NUMBER() OVER函数的基本用法用法

    语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW ...

  4. crawler_URL编码原理详解

    经常写爬虫的童鞋,难免要处理含有中文的url,大部分时间,都知道url_encode,各个语言也都有支持,今天简单整理下原理,供大家科普 1.特征: 如果URL中含有非ASCII字符的话, 浏览器会对 ...

  5. 嵌Ruby 2 《捆绑》

    本章主要介绍 Ruby Object 与C++对象绑定 //====================================================================== ...

  6. c++中&amp;和&amp;&amp;有什么差别

    他们不同点在于&&相当一个开关语句,就是说假设&&前面值为false那么他就不继续运行后面的表达式:而&无论前面的值为什么,总是运行其后面的语句. &能 ...

  7. 安裝 Rails 開發環境

    安裝 Rails 開發環境 Give someone a program, you frustrate them for a day; teach them how to program, you f ...

  8. java语言内部类和匿名内部类

    内部类 在类定义也有类,在该类上的内部被称为一个内部类. 访问功能: 1,内部类可以直接访问外部类成员,它包含私有成员 2,外部类需要访问内部类的成员将需要建立一流的内部对象. 一般用于类的设计. 分 ...

  9. POJ 2724 Purifying Machine(最大独立集)

    POJ 2724 Purifying Machine 题目链接 题意:这题题意有点没看懂.看了别人的题解, 给出m串长度为n的01串. 有些串中可能包括,这种串能够表示两个串,为1 和为0. 反复的算 ...

  10. Android注入事件的三种方法比较

    方法1:使用内部APIs 该方法和其他所有内部没有向外正式公布的APIs一样存在它自己的风险.原理是通过获得WindowManager的一个实例来访问injectKeyEvent/injectPoin ...