Follow up for “Unique Paths”:

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.

[[0,0,0],[0,1,0],[0,0,0]
]

The total number of unique paths is 2.

寻求最短路径,从左上走到右下,保证每次只能往左走或往下走(不可以斜着走)。其中数字1是障碍,表示“此路不通”,求总共的路线数。

第一种二维数组

用一个二维数组来表示前者的路径
核心就是这个,如果不等于1,我们就找到前者的路径相加。

if (obstacleGrid[i][j] == 1) {
                    continue;
                } else {                    int tmp = obstacleGrid[i - 1][j] == 1 ? 0 : val[i - 1][j];
                    tmp = obstacleGrid[i][j - 1] == 1 ? tmp : tmp
                            + val[i][j - 1];
                    val[i][j] = tmp;
                }
public int uniquePathsWithObstacles1(int[][] obstacleGrid) {if (obstacleGrid[0][0] == 1)return 0;int m = obstacleGrid.length;int n = obstacleGrid[0].length;int[][] val = new int[m][n];val[0][0] = 1;for (int i = 1; i < m; i++)if (obstacleGrid[i][0] != 1 && val[i - 1][0] != 0)val[i][0] = 1;for (int i = 1; i < n; i++)if (obstacleGrid[0][i] != 1 && val[0][i - 1] != 0)val[0][i] = 1;for (int i = 1; i < m; i++) {for (int j = 1; j < n; j++) {if (obstacleGrid[i][j] == 1) {continue;} else {int tmp = obstacleGrid[i - 1][j] == 1 ? 0 : val[i - 1][j];tmp = obstacleGrid[i][j - 1] == 1 ? tmp : tmp+ val[i][j - 1];val[i][j] = tmp;}}}return val[m - 1][n - 1];}

第二种一维数组

其实一维数组足以表示前者的路径,因为一维数组左边是你更新过的,右边是没更新,没更新的相当于上一排,也就是上一排的来路加上左边的来路之和就是现在的来路。(解释好混乱,但我是这样想就理解了)

public int uniquePathsWithObstacles(int[][] obstacleGrid) {if (obstacleGrid[0][0] == 1)return 0;int m = obstacleGrid.length;int n = obstacleGrid[0].length;int[] step = new int[n];step[0] = 1;for (int i = 0; i < m; i++)for (int j = 0; j < n; j++) {if (obstacleGrid[i][j] == 1)step[j] = 0;else if (j > 0)step[j] += step[j - 1];}return step[n - 1];}

[LeetCode]--63. Unique Paths II相关推荐

  1. 【动态规划】LeetCode 63. Unique Paths II

    LeetCode 63. Unique Paths II Solution1:我的答案 在哪里做过这题? class Solution { public:int uniquePathsWithObst ...

  2. [Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II

    115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Fo ...

  3. LeetCode 63. Unique Paths II

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

  4. 63. Unique Paths II and 64. Minimum Path Sum

    文章目录 1 63 Unique Paths II 1.1 题目描述 1.2 动态规划解决 2 64. Minimum Path Sum 2.1 题目理解 2.2 动态规划 这一遍刷dp的题目就很轻松 ...

  5. 63. Unique Paths II

    和Unique paths是一样的 1 public int uniquePathsWithObstacles(int[][] obstacleGrid) { 2 if(obstacleGrid == ...

  6. [LeetCode][Java] Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  7. 63. Unique Paths II 动态规划

    description: https://leetcode.com/problems/unique-paths/ 机器人从一堆方格的左上角走到右下角,只能往右或者往下走 ,问有几种走法,这个加了难度, ...

  8. 63. Unique Paths II 不同路径 II

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

  9. 【动态规划】LeetCode 62. Unique Paths

    LeetCode 62. Unique Paths Solution1:我的未能AC的答案 递归超时了!!! class Solution { public:int uniquePaths(int m ...

最新文章

  1. spring oauth2+JWT后端自动刷新access_token
  2. android 之使用多线程中的AsyncTask实现下载网络图片资源
  3. Spring Boot 入门(五):集成 AOP 进行日志管理
  4. mysql 1280_技术分享 | MySQL 一次奇怪的故障分析
  5. Java Web学习总结(42)——JavaEE常用的13种核心API与组件
  6. 直接学python3_新手应该学python2还是python3?
  7. 三网 —— 计算机网络、电信网络、广播电视网络(移动网络)
  8. 蓝桥杯2013c++真题:颠倒的价牌
  9. 国产常用GIS工具软件
  10. elementUI 输入框添加小图标
  11. 显示农历天气时钟小部件下载_文字云时钟安卓版下载|文字云时钟app下载_v1.0
  12. html5猜数字游戏代码,js猜数字小游戏的简单实现代码
  13. 诚信迎考 计算机考试主题班会策划,诚信考试主题班会策划书
  14. [tcpreplay] tcpreplay高级用法--使用tcpreplay-edit进行循环动态发包
  15. 黑马程序员_JAVA相关基础知识
  16. Android应用全屏显示
  17. 转:关于C++ const 的全面总结
  18. 小程序之旅(8) wx.qy.login 坑
  19. SaaS、PaaS、IaaS简介
  20. 鼠标移动代码(使用光标健移动)

热门文章

  1. 使用Docker打包发布Django应用
  2. Spring mvc整合freemarker详解
  3. 【软件构造】第二章 软件构建的过程和工具(2)
  4. 用python 10min手写一个简易的实时内存监控系统
  5. 一步一步学习iOS 5编程(第三版)-PDF中文版-正式发布!
  6. 十万腾讯人,自救1000天
  7. 社交类产品设计的9个点,整不好会挨怼~
  8. PMCAFF微分享 | 阿檬:如何设计好工具型软件?产品经理必备技能
  9. 幕课网产品总监:教你从0到1打造600W下载量的爆款APP
  10. 【Party】现在只是一张普通的照片,以后可能就是历史。