【Unique Paths II】cpp
题目:
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的更多相关文章
- leetcode 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【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 ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【Combination Sum II 】cpp
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【Word Ladder II】cpp
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【palindrome partitioning II】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【Jump Game II 】cpp
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 【Single Num II】cpp
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
随机推荐
- linux 命令——51 lsof(转)
lsof(list open files) 是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以 如传输控制 ...
- NOIP2018初赛 解题报告
前言 \(NOIP2018\)初赛已经结束了,接下来就要准备复赛了. 不过,在此之前,还是先为初赛写一篇解题报告吧. 单项选择题 送分题.(虽然我还是做错了)可以考虑将它们全部转化为\(10\)进制, ...
- 【BZOJ1833】[ZJOI2010] count 数字计数(数位DP)
点此看题面 大致题意: 求在给定的两个正整数\(a\)和\(b\)中的所有整数中,\(0\sim9\)各出现了多少次. 数位\(DP\) 很显然,这是一道数位\(DP\)题. 我们可以用前缀和的思想, ...
- sessionStorage 和 localStorage
html5 中的 web Storage 包括了两种存储方式:sessionStorage 和 localStorage. sessionStorage 用于本地存储一个会话(session)中的数据 ...
- 剑指Offer的学习笔记(C#篇)-- 和为S的连续正数序列
题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...
- 记一次加密算法MD5
通过MessageDigest可以获取到16个字节数组: MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] ...
- 基于纹理内存的CUDA热传导模拟
原文链接 项目中有三个,第一个是全局内存,其余两个分别是基于1d和2d纹理内存.项目打包下载. 纹理内存是只读内存,与常量内存相同的是,纹理内存也缓存在芯片中,因此某些情况下,它能减少对内存的请求并提 ...
- PHP中可变变量到底有什么用?
转自:http://blog.csdn.net/engine_1124/article/details/8660291 什么是可变变量? PHP提供了一种其他类型的变量——可变变量.可变变量允许我们动 ...
- Java第六次作业:RuPengGame setGameSize setGameTitle alert loadBgView playSound pause closeSound confirm input createText setTextPosition setTextColor setTextFontSize hideText showText CreateImage(number)
package com.swift; import java.awt.Color; import com.rupeng.game.GameCore;//导入游戏引擎包 //实现Runnable接口 p ...
- 利用Filter解决跨域请求的问题
1.为什么出现跨域. 很简单的一句解释,A系统中使用ajax调用B系统中的接口,此时就是一个典型的跨域问题,此时浏览器会出现以下错误信息,此处使用的是chrome浏览器. 错误信息如下: jquery ...