- 浏览: 188330 次
- 性别:
- 来自: 济南
-
文章分类
最新评论
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.
这道题目是Unique Paths的变形题目,用1代表障碍物,有障碍物的地方就是死路,同样采用动态规划的思想,只是多了一些限定条件,当为1时,我们就把当前点的值设定为0;当为0的时候,如果此时(i = 0或者j = 0)这时当前点的值等于它前面的点的值(相当于初始化的过程),如果此时(i > 0并且j > 0),动态转移方程为dp[i][j] = dp[i - 1][j] + dp[i][j - 1],(i >= 1, j >= 1)。代码如下:
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.
这道题目是Unique Paths的变形题目,用1代表障碍物,有障碍物的地方就是死路,同样采用动态规划的思想,只是多了一些限定条件,当为1时,我们就把当前点的值设定为0;当为0的时候,如果此时(i = 0或者j = 0)这时当前点的值等于它前面的点的值(相当于初始化的过程),如果此时(i > 0并且j > 0),动态转移方程为dp[i][j] = dp[i - 1][j] + dp[i][j - 1],(i >= 1, j >= 1)。代码如下:
public class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { if(obstacleGrid == null || obstacleGrid.length == 0) return 0; int m = obstacleGrid.length; int n = obstacleGrid[0].length; for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) { if(obstacleGrid[i][j] == 1) obstacleGrid[i][j] = 0; else if (i == 0 && j == 0) obstacleGrid[0][0] = 1; else if(i == 0) obstacleGrid[0][j] = obstacleGrid[0][j - 1]; else if(j == 0) obstacleGrid[i][0] = obstacleGrid[i - 1][0]; else obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]; } return obstacleGrid[m - 1][n - 1]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 288Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 292You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 413Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 395Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 521Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 599Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 503Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 696Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 495The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 450Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 611Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 625Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 451All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 924Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 950Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 625Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 715Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 896Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 808You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 749For a undirected graph with tre ...
相关推荐
在介绍“C语言-leetcode题解之63-unique-paths-ii.c”的内容之前,我们首先需要了解几个背景知识点。首先,C语言是一种广泛使用的计算机编程语言,它以其高效率和灵活性而著称,是计算机科学教育中不可或缺的一部分...
main函数中提供了网格的大小,并调用uniquePaths函数打印出结果。 C语言之所以被广泛应用于解决这类问题,是因为它具有接近硬件的执行效率、丰富的库函数以及对内存的精细控制。通过上述的题解,我们可以看到C语言...
在探讨js-leetcode题解之63-unique-paths-ii.js这一问题时,我们首先要了解这道题目的背景。题目的主要目的是解决一个典型的动态规划问题,题目编号为63,属于LeetCode上的一个算法题目。这道题通常被称作“不同路径...
Unique Paths Unique Paths II Longest Palindromic Substring Interleaving String Triangle Distinct Subsequences Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II...
44. Unique Paths II:与 Unique Paths 类似,但是考虑网格中障碍物的处理。 45. Maximum Sum Subarray:找出一个数字序列中和最大的连续子序列。 46. Maximum Product Subarray:找出一个数字序列中乘积最大的连续...
44. Unique Paths II:不同路径数的计算,其中包含障碍物。 45. Maximum Sum Subarray:计算子数组的最大和。 46. Maximum Product Subarray:计算子数组的最大乘积。 47. Coins in a Line:动态规划解决博弈问题,...
- "不同路径"(Unique Paths)和"不同路径 II"(Unique Paths II)同样是关于路径计算的动态规划问题。 **二分搜索(Binary Search)** 二分搜索算法适用于有序数组或列表的快速查找问题。 - "搜索插入位置"...
62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/03/09 70.Climbing Stairs, 198.House Robber, 55.Jump Game 72.Edit Distance, 97.Interleaving String, 115.Distinct Subsequences 2017/04/24 ...
- Unique Paths / Unique Paths II: 前者计算从矩阵的左上角到右下角的路径数量;后者则考虑了障碍物。 - Minimum Path Sum: 在一个m×n的网格中,找到从左上角到右下角的路径,使得路径上的数字总和最小。 - Plus ...
var uniquePaths = function(m, n) { let dp = new Array(m).fill(0).map(() => new Array(n).fill(0)); // 初始化边界条件 for(let i = 0; i ; i++) { dp[i][0] = 1; } for(let j = 0; j ; j++) { dp[0][j...
Unique Paths II **知识点:** - **问题描述:** - 同 62 题,但网格中存在障碍物。 - **解决方案分析:** - **动态规划:** - 定义 `dp[i][j]` 表示到达 `(i, j)` 的路径数量。 - 如果当前位置有障碍物,则...
1、记忆化 2、二维dp 3、滚动优化
II mod之后,可能数学公式就不能简单地给出答案了。但对我来说,其实和前一题没区别。动态规划处理这种问题,早就是牛刀杀鸡了。。 Single Number 碰巧我知道异或的解法。如果不知道的话,想想还是有点费事的。 ...
63.Unique Paths II 64.最小路径和 70.爬楼梯 72.编辑距离 122. 买卖股票的最佳时机 II 152.最大积子阵列 198.强盗 322.硬币变化 贪心算法 45.跳跃游戏II 55.跳跃游戏 哈希 1.二和 3.最长不重复字符的子串 49.组字谜...
【描述】"leetcode63" 提到的这个问题,全称为“Unique Paths II”,是LeetCode中的一个经典问题。在这个问题中,任务是计算一个机器人从左上角到达右下角有多少种不同的路径。与标准的“Unique Paths”问题相比,...
- **Probe Instrumentation Phase:** In this phase, a unique path ID is assigned to each path. This allows for the identification and tracking of individual paths during program execution. - **Backwalk ...
unique_paths.py(类似于 chess_board.c!)+ unique_paths2.py chess_board.c 效率测试 bin_search.py 素数.py 零知识.py Leetcode:现在很多小方案都是leetcode,我主要是为了好玩。 著名算法:Knuth-Morris-Pratt...
public static int uniquePaths(int rows, int cols, int[][] grid) { int[][] dp = new int[rows][cols]; // 初始化第一列全部为1,表示只有一条路径到达每个点 for (int i = 0; i ; i++) { if (grid[i][0] =...