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. mysql - 缺失范围和连续范围

    初始化数据 # 创建表 DROP TABLE IF EXISTS g; CREATE TABLE g( a INT )ENGINE=INNODB; # 初始化数据 ; ; ; ; ; ; ; ; ; ...

  2. lucene和ElasticSearch基本概念

    lucene基本概念 索引(Index) 对应一个倒排表,一个检索的基本单位.在lucene中就对应一个目录. lucene基本概念 段(Segment) 一个索引可以包含多个段,段与段之间是独立的, ...

  3. 三款Javascript SPAs框架资料整理和总结

    一.框架介绍 RequireJS 资料:http://www.requirejs.cn/RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script>标签的脚本加载步骤.可 ...

  4. Android "adb devices no permissions"

    列出当前连接设备时出现以下情况 [user@dell platform-tools]# ./adb devices List of devices attached ???????????? no p ...

  5. 驱动开发学习笔记. 0.02 基于EASYARM-IMX283 烧写uboot和linux系统

    驱动开发读书笔记. 0.02 基于EASYARM-IMX283 怎么烧写自己裁剪的linux内核?(非所有arm9通用) 手上有一块tq2440,但是不知道什么原因,没有办法烧boot进norflas ...

  6. linux中用户、组的管理(密码管理、权限管理及其修改用户、组)(转)

    process(进程) 1 计算资源 权限 用户(获取资源或服务的凭证或标识) 用户,容器,关联权限:用户组(标识符),方便地指派权限 2 用户.组.权限   安全上下文(secure context ...

  7. Splinter学习--初探1,模拟百度搜索

    Splinter是以Selenium, PhantomJS 和 zope.testbrowser为基础构建的web自动化测试工具,基本原理同selenium 支持的浏览器包括:Chrome, Fire ...

  8. Checkpoints codeforces 709B

    http://codeforces.com/problemset/problem/709/B 题意:给出一条横向坐标轴,给出Vasya所在的坐标位置及其另外n个坐标.Vasya想要至少访问n-1个位置 ...

  9. CSS基础选择器

    1.html负责结构,css负责样式,js负责行为. css是写在head标签里面,容器style标签里面, <style type="text/css"> body{ ...

  10. 搜索引擎系列 ---lucene简介 创建索引和搜索初步

    一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎 :Lucene得名于Doug妻子 ...