原题

思路:

用到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. list 多行表头 表头合并

    http://blog.csdn.net/safedebug/article/details/52971685

  2. flask(三)

    1.cbv的用法 from flask import Flask,views app = Flask(__name__) class Login(views.MethodView ): def get ...

  3. Spark之SparkSql

    -- Spark SQL 以编程方式指定模式 val sqlContext = new org.apache.spark.sql.SQLContext(sc) val employee = sc.te ...

  4. python之数据分析pandas

    做数据分析的同学大部分入门都是从excel开始的,excel也是微软office系列评价最高的一种工具. 但当数据量超过百万行的时候,excel就无能无力了,python第三方包pandas极大的扩展 ...

  5. vmware centos7虚拟机克隆系统如何修改网卡设置?

    1.克隆虚拟机,克隆前需关闭虚拟机2.克隆之后的网卡问题解决,其中需要修改HWADDR和UUID  /etc/sysconfig/network-scripts/ifcfg-ens32  uuid获取 ...

  6. kubernetes实战篇之dashboard搭建

    系列目录 kubernetes dashboard是kubernetes官方提供的web管理界面,通过dashboard可以很方便地查看集群的各种资源.以及修改资源编排文件,对集群进行扩容操作,查看日 ...

  7. 通用shell函数库

    1.输出字体颜色库 #!/bin/bash export black='\E[0m\c' export boldred='\E[1;31m\c' export boldgreen='\E[1;32m\ ...

  8. Git密码修改后,Jenkins job如何批量更新密码?

    很多时候,由于一些原因,更新了Git账号密码:但是,Jenkins构建时,需要通过这个账号去拉取代码:这个时候咋办? 很多同学会说,直接一个个项目更新就OK. 那么,如果是几百个项目.甚至几千个项目呢 ...

  9. Codeforces Gym101246J:Buoys(三分搜索)

    http://codeforces.com/gym/101246/problem/J 题意:给出n个点坐标,要使这些点间距相同的话,就要移动这些点,问最少的需要的移动距离是多少,并输出移动后的坐标. ...

  10. html手机自适应屏幕

    <meta name="viewport" content="height=device-width, initial-scale=1.0, maximum-sca ...