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.

Note: m and n will be at most 100.

分析:

题中给出了m×n的方格,m和n最大值为100,左上角的小机器人只能每次向右走一步或者向下走一步,终点在方格的右下角,

求一共有多少条不同的路径。

最优的子结构: a[i,j] = a[i-1,j] + a[i,j-1]

第一行中,不管到达哪个点,都是沿着第一行一直走才能达到的,如果第一行的值为0,第一行的点的路径为1, a[i][0] = 1;

如果遇到了障碍物,为1的后面的方格都走不到了,所以跳出循环。

第一列中,不管到达哪个点,都是沿着第一列一直走才能达到的,如果方格内的值为0,第一行的点的路径为1, a[0][j] = 1;

如果遇到了障碍物,为1的后面的方格都走不到了,所以跳出循环。

从起点到方格内某个点的不同路径数量可以分解为该点上方的点和左侧的点路径之和。

public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid.length == 0 || obstacleGrid[0].length == 0){
return 0;
}
int n = obstacleGrid.length;
int m = obstacleGrid[0].length;
int[][] a = new int[n][m];
for(int i=0;i<n;i++){
if(obstacleGrid[i][0] == 0){
a[i][0] = 1;
}else{
break;
}
} for(int j=0;j<m;j++){
if(obstacleGrid[0][j] == 0){
a[0][j] = 1;
}else{
break;
}
} for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
if(obstacleGrid[i][j] != 1){
a[i][j] = a[i-1][j] + a[i][j-1];
}else{
a[i][j] = 0;
}
}
}
return a[n-1][m-1];
}
}

leetcode 63. Unique Paths II的更多相关文章

  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 不同的路径之二

    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(Medium)

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

  4. [leetcode] 63. Unique Paths II (medium)

    原题 思路: 用到dp的思想,到row,col点路径数量 : path[row][col]=path[row][col-1]+path[row-1][col]; 遍历row*col,如果map[row ...

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

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

  6. 【LeetCode】63. Unique Paths II

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

  7. 62. Unique Paths && 63 Unique Paths II

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

  8. [Leetcode Week12]Unique Paths II

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

  9. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

随机推荐

  1. Github for Windows使用介绍

    Git已经变得非常流行,连Codeplex现在也已经主推Git.Github上更是充斥着各种高质量的开源项目,比如ruby on rails,cocos2d等等.对于习惯Windows图形界面的程序员 ...

  2. Robot Framework--12 RFS+AutoItLibrary测试web对话框

    转自:http://blog.csdn.net/tulituqi/article/details/21871247 Selenium2library在我们实际测试web页面的时候基本上已经够用了,不过 ...

  3. Lua 之数据结构

    Lua 之数据结构 数组 通过整数下标访问的table中的元素,即是数组,下标默认从1开始. 一个创建二维数组的例子: mt = {} , do mt[i] = {} , do mt[i][j] = ...

  4. python 日志收集系统

    服务器端: #!/usr/bin/env python # -*- coding:utf-8 -*- import socket ip_port = ('0.0.0.0',9999) sk = soc ...

  5. C# 连接mongodb副本集+分片读写分离及学习资料

    一.副本集配置 搭建完毕,1台主实例.1台从实例.1台仲裁实例.mongodb建议副本集中的机器数量为奇数,即至少需要3台实例 二.副本集连接字符串 1.读 mongodb://secondary.c ...

  6. 总结一下安装linux系统经验-版本选择-安装ubuntu

    linux版本选择: 初次接触,建议选 Ubuntu 或者 Fedora,这两个发行版都很容易上手,而且两者都有很强大的中文社区,遇到问题比较容易解决,而且都有国内的源,安装或者更新软件时体验相对会好 ...

  7. html兼容性

    IE  property:value\9; //for all IE IE6 _property:value; IE7 *property:value; IE8 +property:value; IE ...

  8. java.lang.reflect.Method

    java.lang.reflect.Method 一.Method类是什么 Method是一个类,位于java.lang.reflect包下. 在Java反射中 Method类描述的是 类的方法信息, ...

  9. CF461B Appleman and Tree (树DP)

    CF462D Codeforces Round #263 (Div. 2) D Codeforces Round #263 (Div. 1) B B. Appleman and Tree time l ...

  10. web网页颜色色谱

    snow 255 250 250 #fffafa ghostwhite 248 248 255 #f8f8ff whitesmoke 245 245 245 #f5f5f5 gainsboro 220 ...