原题

思路:

用到dp的思想,到row,col点路径数量 :

path[row][col]=path[row][col-1]+path[row-1][col];

遍历row*col,如果map[row][col]为1,则将其置为0;如果非1,则进行上述公式。

最后返回path[终点row][终点col]的值即为解。

一开始的代码,beat 44%,效率不高

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid) {
int rowSize = obstacleGrid.size();
int colSize = obstacleGrid[0].size(); if (obstacleGrid[0][0] == 1) return 0; obstacleGrid[0][0] = 1;
for (int col = 1; col < colSize; col++) {
if (obstacleGrid[0][col] == 1)
obstacleGrid[0][col] = 0;
else
obstacleGrid[0][col] += obstacleGrid[0][col - 1];
}
for (int row = 1; row < rowSize; row++) {
if (obstacleGrid[row][0] == 1)
obstacleGrid[row][0] = 0;
else
obstacleGrid[row][0] += obstacleGrid[row-1][0];
}
for (int i = 1; i < rowSize; i++) {
for (int j = 1; j < colSize; j++) {
if (obstacleGrid[i][j] == 1)
obstacleGrid[i][j] = 0;
else
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
}
}
return obstacleGrid[rowSize - 1][colSize - 1];
}
};

优化后的代码:

使用额外一个数组记录走到每一列有几种走法,因为题目只求终点,则使用一维数组即可。

使用一个额外的整型pre记录当前的前一步有多少种走法。(左边走来+上边走来)

则有:

pre(当前)=dp[col](上一行当前列) + pre(左一格当前行);
dp[col](当前)=pre;

beat 100%

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid) {
int rowSize = obstacleGrid.size();
int colSize = obstacleGrid[0].size();
int dp[colSize] = {0};
int pre = 1;
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
if (obstacleGrid[i][j] == 0) {
pre += dp[j];
dp[j] = pre;
}
else {
pre = 0;
dp[j] = 0;
}
}
pre=0;
}
return dp[colSize-1];
}
};

[leetcode] 63. Unique Paths II (medium)的更多相关文章

  1. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  2. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  3. [LeetCode] 63. Unique Paths II 不同的路径之二

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. leetcode 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  5. LeetCode: 63. Unique Paths II(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/

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

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

  7. 【LeetCode】63. Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  8. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  9. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

随机推荐

  1. 转一个git的命令

    Git远程操作详解   Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Git远程操作. git clone git rem ...

  2. shell多线程之进程间通信(3)

    之前的文章依赖是1对1或1多对的,但每个任务的前置任务都只有1个. 本文的核心在于一个任务依赖于多个任务的执行完成,如上图所示,这个任务就是fact,只有new和dviduser两个任务都完成的情况下 ...

  3. shell多线程之进程间通信

    # 这是一个简单的并发程序,有如下要求: # .有两个程序a和b,希望他们能并发执行,以节约时间 # .a和b都是按照日期顺序执行,但b每日程序的前提条件是当日a的程序已经执行完毕 #解决方案: # ...

  4. Spring Boot:实现MyBatis动态创建表

    综合概述 在有些应用场景中,我们会有需要动态创建和操作表的需求.比如因为单表数据存储量太大而采取分表存储的情况,又或者是按日期生成日志表存储系统日志等等.这个时候就需要我们动态的生成和操作数据库表了. ...

  5. appcan 多按钮提示框

    使用  appcan.window.alert EG: var btnList=new Array(); btnList[0]="确认"; btnList[1]="取消& ...

  6. 44 | 测试先行:测试驱动开发(TDD)

  7. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  8. HDU 1286:找新朋友(欧拉函数)

    http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. #include <cstdio> #include < ...

  9. 获取当前时间的MySql时间函数

    mysql> select current_timestamp(); +---------------------+ | current_timestamp() | +------------- ...

  10. MQ初窥门径【面试必看的Kafka和RocketMQ存储区别】

    MQ初窥门径 全称(message queue)消息队列,一个用于接收消息.存储消息并转发消息的中间件 应用场景 用于解决的场景,总之是能接收消息并转发消息 用于异步处理,比如A服务做了什么事情,异步 ...