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. BZOJ-1045 糖果传递 数学+递推

    1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2975 Solved: 1327 [Submit][Sta ...

  2. BZOJ4034 T2

    Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所 ...

  3. springmvc常用注解标签详解

    1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...

  4. Putty远程登录VMware虚拟机Linux(Ubuntu)

    安装SSH服务 Ubuntu默认并没有安装ssh服务,如果通过ssh链接ubuntu,需要自己手动安装ssh-server.判断是否安装ssh服务,可以通过如下命令进行: www.linuxidc.c ...

  5. Note:JSON

    JSON是JavaScript的原生格式,意味着在JavaScript中处理JSON数据不需要特殊的API或工具包,它是完全独立于语言的文本格式,可以把JavaScript对象中的一组对象转化为字符串 ...

  6. 如何起草你的第一篇科研论文——应该做&避免做

    如何起草你的第一篇科研论文——应该做&避免做 导语:1.本文是由Angel Borja博士所写.本文的原文链接在这里.感谢励德爱思唯尔科技的转载,和刘成林老师的转发.2.由于我第二次翻译,囿于 ...

  7. DedeCMS全版本通杀SQL注入漏洞利用代码

    EXP: Exp:plus/recommend.php?action=&aid=1&_FILES[type][tmp_name]=\'   or mid=@`\'` /*!50000u ...

  8. Java开源数据库管理工具

    SQuirreL SQL Client   SQuirreL SQL Client 是一个用 Java 编写的程序,它允许您查看数据库的内容.发出 SQL 命令,以及如您将看到的,执行许多其他功能.构 ...

  9. Tools下的mdscongiguer 文件中 43行 oracle 配置 发现需要连接库 -lclntsh libclntsh.so 库是个什么东西呢?

    Tools下的mdscongiguer     文件中 43行  oracle 配置      发现需要连接库 -lclntsh      libclntsh.so 库是个什么东西呢? 分想一个知乎网 ...

  10. C++中map的基本操作和使用;

    注:本文来自sina live 的博文 Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本 ...