题目:

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.

代码:

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid)
{
const int m = obstacleGrid.size();
const int n = obstacleGrid[].size();
vector<vector<int> > cache(m+,vector<int>(n+,));
return Solution::dfs(m, n, cache, obstacleGrid);
}
static int dfs( int i, int j, vector<vector<int> >& cache, vector<vector<int> >& obstacleGrid )
{
if ( i< || j< || obstacleGrid[i-][j-]== ) return ;
if ( i== && j== ) return ;
return Solution::getDFS(i-, j, obstacleGrid, cache) + Solution::getDFS(i, j-, obstacleGrid, cache);
}
static int getDFS(int i, int j, vector<vector<int> >& obstacleGrid, vector<vector<int> >& cache)
{
if ( cache[i][j]> ){
return cache[i][j];
}
else{
return cache[i][j] = Solution::dfs(i, j, cache, obstacleGrid);
}
}
};

tips:

上述的代码采用了深搜+缓存(“备忘录”)法。照比Unique Paths多了一个判断条件,如果obstacleGrid上该位置为1则直接返回0。

有个细节上的技巧:

obstacleGrid是题目给出的定义,下标[m-1][n-1]

cache是自定义的缓存数组,下标[m][n]

同样的位置,cache的坐标比obstacleGrid大1;因此,只要深搜过程中保证了cache的坐标在合理范围内,无论是横坐标-1还是纵坐标-1,映射到cache的坐标中总不会越界。

上述代码的效率并不高,但是比较容易处理各种case。再尝试动态规划的解法。

==========================================

在Unique Paths的基础上,用动规又把Unqiue Paths II写了一遍。

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid)
{
const int m = obstacleGrid.size();
const int n = obstacleGrid[].size();
vector<int> dp(n, );
if ( obstacleGrid[][]== ) return ;
dp[] = ;
for ( size_t i = ; i < m; ++i )
{
dp[] = obstacleGrid[i][]== ? : dp[];
for ( size_t j =; j < n; ++j )
{
dp[j] = obstacleGrid[i][j]== ? : dp[j-] + dp[j];
}
}
return dp[n-];
}
};

tips:

沿用了滚动数组的技巧,额外空间缩减为O(n),代码的效率也提升了。

==========================================

第二次过这道题,用dp过的。

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid.empty()) return ;
const int m = obstacleGrid.size();
const int n = obstacleGrid[].size();
int dp[m][n];
fill_n(&dp[][], m*n, );
for ( int i=; i<n; ++i )
{
if ( obstacleGrid[][i]== )
{
dp[][i]=;
}
else
{
break;
}
}
for ( int i=; i<m; ++i )
{
if ( obstacleGrid[i][]== )
{
dp[i][]=;
}
else
{
break;
}
}
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-];
}
};

【Unique Paths II】cpp的更多相关文章

  1. leetcode 【 Unique Paths II 】 python 实现

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  2. 【Path Sum II】cpp

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  3. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  4. 【Combination Sum II 】cpp

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  5. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  6. 【Spiral Matrix II】cpp

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  7. 【palindrome partitioning II】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  8. 【Jump Game II 】cpp

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  9. 【Single Num II】cpp

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

随机推荐

  1. [转] Adobe acrobat 破解教程

    最新版的Adobe Acrobat DC Pro可以使我们更方便的管理和编辑PDF文档,现在我为大家带来Adobe Acrobat DC Pro安装及破解教程,供大家安装和使用. 工具/原料   Ad ...

  2. mybatis-关联关系2

    关系关系主要有一对一,一对多,多对多,往往多对多都是通过俩个一对多来完成的 实例项目还是之前的,只是增加了一个年级实体类 1.创建年级实体类:---年级中有学生的集合 package com.java ...

  3. STL容器 成员函数 时间复杂度表

    Sequence containers Associative containers   Headers <vector> <deque> <list> <s ...

  4. Python中的集合set

    >>> help(set) Help on class set in module __builtin__: class set(object) | set(iterable) -- ...

  5. C# 理解FileInfo类的Open()方法

    我们在对文件进行读写操作之前必须打开文件,FileInfo类为我们提供了一个Open()方法,该方法包含了两个枚举类型值的参数,一个为FileMode枚举类型值,另一个为FileAccess枚举类型值 ...

  6. Java异常处理的9个最佳实践

    无论你是新手还是资深程序员,复习下异常处理的实践总是一件好事,因为这能确保你与你的团队在遇到问题时能够处理得了它. 在 Java 中处理异常并不是一件易事.新手觉得处理异常难以理解,甚至是资深开发者也 ...

  7. Vue 父组件传值到子组件

    vue 父组件给子组件传值中 这里的AccessList就是子组件 如果 是静态传值的话直接  msg="xxx"就好 这里动态取值的话就  :msg=xxxxx ________ ...

  8. mysql 删除 一天前 创建 的数据,计算时间差

    DELETE from table_name WHERE TIMESTAMPDIFF(SECOND ,CREATE_TIME,now() ) > 24*60*60 https://www.cnb ...

  9. Python_三级目录

    程序要求: 1. 使用字典存储 1. 可以一层一层的进入到所有层2. 可以在每层返回上一层3. 可以在任意层退出 三级目录写了两个版本,第一个版本是刚看完字典写出来的,代码很多冗余,很多重复. men ...

  10. python笔记-for循环的方法

    #!/usr/bin/env python #-*- coding:utf-8 -*- ''' for 语句 格式: for 变量名 in 集合: 语句 逻辑:按顺序取"集合"中的 ...