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.

解题思路:

本题如果使用上题中的解法一,将会非常复杂,因此我们修改解法二即可,JAVA实现如下:

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

Java for LeetCode 063 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 Week12]Unique Paths II

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

  3. 【leetcode】Unique Paths II

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

  4. 【LeetCode】063. Unique Paths II

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

  5. Java for LeetCode 062 Unique Paths

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

  6. [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 ...

  7. leetcode 63. Unique Paths II

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

  8. 【题解】【矩阵】【回溯】【Leetcode】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 ...

  9. leetcode 【 Unique Paths II 】 python 实现

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

随机推荐

  1. linux 通过哪个命令可以查看某个服务及其端口、进程号

    netstat/lsof netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况 -a 显示一个所有的有效连接信息列表(包括已建立的连接,也 ...

  2. std::ios::sync_with_stdio(false);

    这句语句是用来取消cin的同步,什么叫同步呢?就是iostream的缓冲跟stdio的同步.如果你已经在头文件上用了using namespace std;那么就可以去掉前面的std::了.取消后就c ...

  3. CCNET配置文件配置工具

    当我们在使用CruiseControl.NET进行配置的时候,你会发现配置文件是个非常头痛的事,无从下手,下面我在google找了一个09年的工具,主要是针对CruiseControl.NET进行配置 ...

  4. Linux中的TUN/TAP设备

    今天才发现这家伙...怎么讲...深以为耻.晚上的任务是加深对它的了解,就这么定了. 1. General questions.1.1 What is the TUN ?  The TUN is Vi ...

  5. myeclipse 引入jar包 (包括 jdbc 驱动引用)

    A.直接用MyEclipse里自带的相关的项目jar包,右击项目"MyEclipse"菜单,选择对应的jar包就OK了 B.添加外部的jar包到web项目的lib包下,右击项目&q ...

  6. 从注册流程 分析如何安全退出多个Activity 多种方式(附DEMO)

      退出Activity注册Android遍历   目录(?)[+] 前言 知识结构 具体方案 方案1 方法采用FLAG_ACTIVITY_CLEAR_TOP退出整个程序多activity 方案2 方 ...

  7. 软件开发过程中的审查 (Review)

    http://blog.csdn.net/horkychen/article/details/5035769 软件开发过程中的审查 (Review)   希望别人做些什么->定义出流程 希望别人 ...

  8. Centos目录结构详细版

    使用linux也有一年多时间了  最近也是一直在维护网站系统主机  下面是linux目录结构说明 本人使用的是centos系统,很久没有发表博文了 近期会整理自己所用所了解知识点,发表linux相关的 ...

  9. ASP.NET MVC 站点设置.html 为起始页

    1.  删除 controller="XX" 2. 确保你的工程根目录下的*.htm或*.html文件名在IIS默认文档中存在 搞定

  10. java分布式通信系统(J2EE分布式服务器架构)

    一.序言 近几个月一直从事一个分布式异步通信系统,今天就整理并blog一下. 这是一个全国性的通信平台,对性能,海量数据,容错性以及扩展性有非常高的要求,所以在系统的架构上就不能简单的采用集中式.简单 ...