Leetcode63. Unique Paths II
2016-07-04 09:44
483 查看
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
in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
The total number of unique paths is
Note: m and n will be at most 100.
这个题是62题的升级版,方法和62题一样,采用动态规划。只是当我们遇到障碍物的格子的时候要将其赋值0,,就行了。
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid.length==0){
return 0;
}
int m =obstacleGrid.length;
int n = obstacleGrid[0].length;
if(n==0){
return 0;
}
if(obstacleGrid[0][0]==1){
return 0;
}
int[][] stepsGrid = new int[m]
;
for(int i=0; i< m ;i++){
if(obstacleGrid[i][0]==1){
break;
}
stepsGrid[i][0] =1;
}
for(int i=0; i<n; i++){
if(obstacleGrid[0][i]==1){
break;
}
stepsGrid[0][i] =1;
}
for(int i=1; i<m; i++){
for(int j=1; j<n; j++){
if(obstacleGrid[i][j]==1){
stepsGrid[i][j]=0;
}else{
stepsGrid[i][j]= stepsGrid[i-1][j]+stepsGrid[i][j-1];
}
}
}
return stepsGrid[m-1][n-1];
}
}
时间复杂度O(m*n),空间复杂度也是O(m*n), 论坛上有空间复杂度为O(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
1and
0respectively
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.
这个题是62题的升级版,方法和62题一样,采用动态规划。只是当我们遇到障碍物的格子的时候要将其赋值0,,就行了。
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid.length==0){
return 0;
}
int m =obstacleGrid.length;
int n = obstacleGrid[0].length;
if(n==0){
return 0;
}
if(obstacleGrid[0][0]==1){
return 0;
}
int[][] stepsGrid = new int[m]
;
for(int i=0; i< m ;i++){
if(obstacleGrid[i][0]==1){
break;
}
stepsGrid[i][0] =1;
}
for(int i=0; i<n; i++){
if(obstacleGrid[0][i]==1){
break;
}
stepsGrid[0][i] =1;
}
for(int i=1; i<m; i++){
for(int j=1; j<n; j++){
if(obstacleGrid[i][j]==1){
stepsGrid[i][j]=0;
}else{
stepsGrid[i][j]= stepsGrid[i-1][j]+stepsGrid[i][j-1];
}
}
}
return stepsGrid[m-1][n-1];
}
}
时间复杂度O(m*n),空间复杂度也是O(m*n), 论坛上有空间复杂度为O(1) 的,思路相同,只是在输入数组上修改。
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解