Java for LeetCode 063 Unique Paths II
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的更多相关文章
- 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). ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 【LeetCode】063. Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 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 ...
- [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 ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【题解】【矩阵】【回溯】【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 ...
- leetcode 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
随机推荐
- 【HDU 5363】Key Set
题 Description soda has a set $S$ with $n$ integers $\{1, 2, \dots, n\}$. A set is called key set if ...
- 21.Android之SQLite数据库学习
Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLite 只需要带一个动 ...
- poj 2155 Matrix---树状数组套树状数组
二维树状数组模版,唯一困难,看题!!(其实是我英语渣) Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22098 ...
- 黑客帝国风格必备插件ProPowerTools
ProPowerTools 详细说明点这里
- 在excel批量更改单元格类型后的批量刷新显示
把E的东西变成完整显示 解决办法: 选中所需要更改的整列数据------>菜单栏的数据选项------>分列
- Sublime text 快捷键总结
下述快捷键都是我写C++代码时发现的,是否适用其他格式(扩展名)的文件尚为未知. Basic Editing Ctrl + A 全选 Ctrl + S 保存 Ctrl + C 复制 Ctrl + V ...
- ISO 基础之 (十三) protocol 协议
一 简绍 protocol,简单来说就是一系列不属于任何类的方法列表,其中声明的方法可以被任何类实现.这种模式一般称为代理(delegation)模式.通过Protocol定义各种行为,在不同的场景采 ...
- JS 事件练习
QQ拖拽及状态栏选择 HTML <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...
- JS实现打字机式字符输出效果
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Object学习笔记
<script type="text/javascript"> function forEach(o){ var html =""; for(var ...