62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/
这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23*12时就超时了:
class Solution {
public:
// Solution():dp1(m,vector<int>(n,-1)),dp2(m,vector<int>(n,-1)){
// }
int uniquePaths(int m, int n) {
helper(,,m,n,dp1,dp2);
return res;
}
void helper(int row,int col,int m,int n,vector<vector<int>>& dp1,vector<vector<int>>& dp2){
if(row==m && col==n){
res++;
return;
}
if(row<m&&col<n){
helper(row+,col,m,n,dp1,dp2);
helper(row,col+,m,n,dp1,dp2);
}
else if(row==m && col<n){
helper(row,col+,m,n,dp1,dp2);
}
else if(row<m && col==n){
helper(row+,col,m,n,dp1,dp2);
}
}
private:
vector<vector<int>> dp1;
vector<vector<int>> dp2;
int res;
};
利用动态规划来做,储存一个二维数组:vector<vector<int>> dp,用来保存每一个位置的走法总数:
则dp[i][j]=dp[i-1][j]+dp[i][j-1],代码如下:
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m+,vector<int>(n+,-));
helper(,,m,n,dp);
return dp[m][n];
}
void helper(int row,int col,int m,int n,vector<vector<int>>& dp){
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
if(i==||==j)
dp[i][j]=;
else{
dp[i][j]=dp[i-][j]+dp[i][j-];
}
}
}
}
};
63 Unique Paths II https://leetcode.com/problems/unique-paths-ii/
递归公式仍旧是上一个,不过遇到障碍要作相应处理(置为零)
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int row=obstacleGrid.size();
int col=obstacleGrid[].size();
vector<vector<int>> dp(row,vector<int>(col,-));
for(int i=;i<row;i++){
for(int j=;j<col;j++){
if(obstacleGrid[i][j]==){
dp[i][j]=;
}
else{
if(i==&&==j){
dp[i][j]=;
}
else if(==i && j>){
dp[i][j]=dp[i][j-];
}
else if(j==&& i>){
dp[i][j]=dp[i-][j];
}
else{
dp[i][j]=dp[i-][j]+dp[i][j-];
}
}
}
}
return dp[row-][col-];
}
};
62. Unique Paths && 63 Unique Paths II的更多相关文章
- <LeetCode OJ> 62. / 63. Unique Paths(I / II)
62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 【leetcode】62.63 Unique Paths
62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the di ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)
Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...
- Java实现 LeetCode 63 不同路径 II(二)
63. 不同路径 II 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在 ...
- 刷题-力扣-63. 不同路径 II
63. 不同路径 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/unique-paths-ii/ 著作权归领扣网络所有.商业转 ...
- leetcode@ [62/63] Unique Paths II
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
随机推荐
- Android_layout 布局(二)
昨天学习了layout 布局的线性布局和相对布局. 今天我们学习剩余的三个布局,分别是: 一.帧布局(FrameLayout) 在这个布局中,所有的子元素都不能被指定放置的位置,它们通通放于这块区域的 ...
- iOS10以及xCode8相关资料收集
兼容iOS 10 资料整理笔记 源文:http://www.jianshu.com/p/0cc7aad638d9 1.Notification(通知) 自从Notification被引入之后,苹果就不 ...
- 写css时要注意数字的浮动方向
写css时要注意数字的浮动方向 当数字位数增加时他的方向才是正确的 text-align:right;padding-right:29px;
- Python常用模块学习
1.模块介绍 2.time & datetime模块 3.random 4.os 5.sys 6.shutil 7.json&pickle 8.shelve 9.xml处理 10.ya ...
- 执行超过1个小时的SQL语句
SELECT MO.MO_ID, MO.ITEM, MO.QTYORDERED, MO.PLANNEDSTARTDATE, BR.MAXLOTSIZE FROM TEMP_MO MO, (SELECT ...
- C# 模拟键盘操作--SendKey(),SendKeys()
模拟键盘输入就是使用以下2个语法实现的.SendKeys.Send(string keys); //模拟汉字(文本)输入SendKeys.SendWait(string keys); //模拟按键输 ...
- Xilium.CefGlue CEF Chrome 自动上传文件不弹出对话框 CefDialogHandler
关键代码如下,如何使用Handler,还不清楚的请继续搜索 internal sealed class WyzCefDialogHandler : CefDialogHandler { ...
- 一个的unity学习系列的博客
1.http://my.csdn.net/caoboya 2.http://my.csdn.net/OnafioO
- JavaWeb 学习009-4个页面,5条sql语句(添加、查看、修改、删除)
===========++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==+++++++++ 2016-12-3------ ...
- php_access_ADOConn 备忘
1.咕~~(╯﹏╰)b 各种乱码. 2.本来是MFC+Access 用msado15.dll写的系统,闲的想用php改改. Demo.php <meta http-equiv="Con ...