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的更多相关文章

  1. &lt;LeetCode OJ&gt; 62. / 63. Unique Paths(I / II)

    62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...

  2. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  3. 【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 ...

  4. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

  5. Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)

    Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...

  6. Java实现 LeetCode 63 不同路径 II(二)

    63. 不同路径 II 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在 ...

  7. 刷题-力扣-63. 不同路径 II

    63. 不同路径 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/unique-paths-ii/ 著作权归领扣网络所有.商业转 ...

  8. leetcode@ [62/63] Unique Paths II

    class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...

  9. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...

随机推荐

  1. c++ 二维数组传递

    c++ 二维数组传递 我们在传递二维数组时,对于新手来说,可能会存在某些问题,下面讲解几种传递方法 在讲解如何传递二维数组时,先看看如何动态new 二维数组 // 二维数组动态申请 int row , ...

  2. 多线程爬取 threading.Thread 文件名支持gbk编码

    # - *- coding:utf-8-*-import urllib2import reimport osimport threadingimport sysreload(sys)sys.setde ...

  3. npm isArray源码

    module exports的是一个函数. !! val是为了将val转化成布尔值. var isArray = Array.isArray; /** * toString */ var str = ...

  4. error C4996: 'fopen': This function or variable may be unsafe.

    vs2013中错误提示信息: error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s ...

  5. TOMCAT的安装部署配置(配图解)

    TOMCAT的安装部署配置 前提已经成功搭建配置JDK 下载好压缩包后,直接解压至某一目录下,目录中不能包含中文 双击安装文件,出现如下界面 点击[NEXT],出现如下界面 点击[I AGREE],出 ...

  6. Spring HTTP下载

    (1)HTTP 协议可以在客户端和服务器之间传递任何类型的文件. HTTP协议下载文档到客户端时候, 必须通过响应头Content-Type设置文件类型. 例如: contentType=text/h ...

  7. C#获得客户端IP

    代码: /// <summary> /// 获得当前页面客户端的IP /// </summary> /// <returns>当前页面客户端的IP</retu ...

  8. js使用Switch达到切换不同颜色的效果

    实现的效果,点击哪个,哪个变颜色,效果如下. 代码如下: <!DOCTYPE html> <html> <head> <meta charset=" ...

  9. IIS上虚拟目录下站点的web.config与根站点的web.config冲突解决方法

    IIS7.5上在站点下部署虚拟目录,访问虚拟目录下的项目提示与父节点配置冲突.,节点与的<system.web>节点与主站点的<system.web>冲突解决方法: 在站点下的 ...

  10. zabbix3.0 安装Tips

    原文转自:http://www.cnblogs.com/tae44/p/4812190.html#3270843 此处只能留空,否则,提示安装无法进行!!