LeetCode -- Unique Paths II
2015-12-01 09:45
453 查看
题目要求:
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.
就是在一个矩阵中,求从左上到右下单元的路径个数,每次只能向下或向右移动。0表示可以通过,1表示障碍。
思路:
本题依然是1个DP问题。设dp[i,j]表示从左上到矩阵的[i,j]位置的路径个数。
1. 当行或列只有1时,如果有障碍,则返回0,否则返回1。
当row > 1 且 col > 1:
2.1 分别初始化dp的第一行(i)和第一列(j),即dp[i,0]和dp[0,j]:如果发现障碍,即i==1或j==1,把则dp[i...n,0]和dp[0,j...n]设为0,没有发现障碍时初始化为1。
2.2 如果obstacleGrid[i,j]为1,即当前为障碍,则dp[i,j]为0,否则:
dp[i, j] = dp[i-1, j] + dp[i, j-1];
实现代码:
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.
就是在一个矩阵中,求从左上到右下单元的路径个数,每次只能向下或向右移动。0表示可以通过,1表示障碍。
思路:
本题依然是1个DP问题。设dp[i,j]表示从左上到矩阵的[i,j]位置的路径个数。
1. 当行或列只有1时,如果有障碍,则返回0,否则返回1。
当row > 1 且 col > 1:
2.1 分别初始化dp的第一行(i)和第一列(j),即dp[i,0]和dp[0,j]:如果发现障碍,即i==1或j==1,把则dp[i...n,0]和dp[0,j...n]设为0,没有发现障碍时初始化为1。
2.2 如果obstacleGrid[i,j]为1,即当前为障碍,则dp[i,j]为0,否则:
dp[i, j] = dp[i-1, j] + dp[i, j-1];
实现代码:
public class Solution { public int UniquePathsWithObstacles(int[,] obstacleGrid) { var row = obstacleGrid.GetLength(0); var col = obstacleGrid.GetLength(1); if(row < 1 || obstacleGrid[0,0] == 1){ return 0; } if(row == 1){ for(var i = 0; i < col; i ++){ if(obstacleGrid[0,i] == 1){ return 0; } } return 1; } if(col == 1){ for(var i = 0; i < row; i ++){ if(obstacleGrid[i,0] == 1){ return 0; } } return 1; } var dp = new int[row, col]; for(var i = 0;i < col; i++){ if(obstacleGrid[0,i] == 1){ while(i < col){ dp[0,i] = 0; i++; } break; } else{ dp[0,i] = 1; } } for(var i = 0;i < row; i++){ if(obstacleGrid[i,0] == 1){ while(i < row){ dp[i,0] = 0; i++; } break; } else{ dp[i,0] = 1; } } for(var i = 1;i < row; i++){ for(var j = 1;j < col; j++){ if(obstacleGrid[i,j] == 1){ dp[i,j] = 0; } else{ dp[i, j] = dp[i-1, j] + dp[i, j-1]; } } } return dp[row-1, col-1]; } }
相关文章推荐
- leetcode -- Unique Binary Search Trees II
- iOS 解决xib加载UITableViewCell的宽度不适应问题
- BlueprintListener的使用
- 【Qt Warning】No1:Debug build
- rabbitmq-queueDeclarePassive
- 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)
- [LeetCode]Longest Increasing Subsequence
- [APUE]再读之文件和目录
- mosquitto ubuntu下编译和安装
- 【代码笔记】16进制颜色与UIColor互转
- 【代码笔记】16进制颜色与UIColor互转
- 10个顶级的CSS UI开源框架
- UIStepper 的简单实用
- UIProgressView用法详解
- java中的StringBuilder
- CALayer基础
- UEFI 系统重装 磁盘转换
- webservice 学习笔记之 Web Service SoapUI 测试工具教程
- UIScrollView,contentOffset,contentInsert的各自特点和区别?
- 《apue》读书笔记 第四章 文件和目录(3)