当前位置:网站首页>LeetCode 沙胡同系列 -- 63. 不同路径 II
LeetCode 沙胡同系列 -- 63. 不同路径 II
2022-07-25 23:56:00 【在河之洲木水】
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish”)。
现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
网格中的障碍物和空位置分别用 1 和 0 来表示。
示例 1:
输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
输出:2
解释:3x3 网格的正中间有一个障碍物。
从左上角到右下角一共有 2 条不同的路径:
1. 向右 -> 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右 -> 向右
示例 2:
输入:obstacleGrid = [[0,1],[0,0]]
输出:1
提示:
m == obstacleGrid.length
n == obstacleGrid[i].length
1 <= m, n <= 100
obstacleGrid[i][j] 为 0 或 1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/unique-paths-ii
思路:
定义二维数组 dp[][] ,dp[i][j] 表示从左上角到 网格 (i,j) 位置一共有多少条路径
1. 初始化
1.1) dp[0][0] = 1
1.2) dp[0][j] j > 0 && j < n,
若是 grid[0][j] = 0 ,则 dp[0][j] = 1,
若是 grid[0][j] = 1,dp[0][j] = 0, ..., dp[0][n-1] = 0
1.3) dp[i][0], i > 0 && i < m
若是 grid[i][0] = 0 ,则 dp[i][0] = 1,
若是 grid[i][0] = 1,dp[i][0] = 0, ..., dp[m-1][n0] = 0
2. 状态转移
若是 grid[i][j] == 1, 则 dp[i][j] = 0; 此路不通
若是 grid[i][j] == 0, dp[i][j] = dp[i-1][j] + dp[i-1][j]
3. 返回结果 dp[m-1][n-1]java代码:
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
// 1. 初始化
boolean flag1 = true;
for (int i = 0; i < m; i++) {
if (flag1 && obstacleGrid[i][0] == 0) {
dp[i][0] = 1;
} else {
flag1 = false;
}
}
boolean flag2 = true;
for (int j = 0; j < n; j++) {
if (flag2 && obstacleGrid[0][j] == 0) {
dp[0][j] = 1;
} else {
flag2 = false;
}
}
// 2. 状态转移
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if(obstacleGrid[i][j] == 1) {
dp[i][j] = 0; // 此路不通
} else {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
}
// 3. 返回结果
return dp[m-1][n-1];
}
}边栏推荐
- Responsibility chain model of behavioral model
- Leetcode 0135. distribute candy
- The process of finding free screen recording software - I didn't expect win10 to come with this function
- Good news under the epidemic
- [learning notes] solid works operation record
- ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
- Topsis与熵权法
- Programming password guessing game
- 行为型模式之观察者模式
- Leetcode 0136. numbers that appear only once: XOR
猜你喜欢

Ratio of learning_ add,ratio_ subtract,ratio_ multiply,ratio_ Use of divide

Article 75: writing skills of academic papers

二叉树——404. 左叶子之和

Topsis与熵权法

Yolov3 trains its own data set

抽丝剥茧C语言(高阶)程序环境和预处理

initializer_ List tool library learning

老旧笔记本电脑变服务器(笔记本电脑+内网穿透)

S4/hana mm & SD EDI Nast based integrated configuration (orders, ordrsp, desadv, invoice)

Read the field status of account in ABAP code (hidden, optional, required)
随机推荐
反射之类加载过程
R language installation tutorial | graphic introduction is super detailed
Matchmaker's words
Programming password guessing game
@Autowired注解的底层原理
SIGIR‘22 推荐系统论文之图网络篇
二叉树——110. 平衡二叉树
Unexpected dubug tricks
Part 74: overview of machine learning optimization methods and superparameter settings
How does JS judge whether the current date is within a certain range
Array merge method: concat()
Several ways of writing strings in reverse order
Fixed and alternate sequential execution of modes
arcgis根据矢量范围裁取tif影像(栅格数据)、批量合并shp文件、根据矢量范围裁取区域内的矢量,输出地理坐标系、转换16位TIF影像的像素深度至8位、shp文件创建和矢量框标绘设置
C language (high level) program environment and preprocessing
注解@Autowired源码解析
1223. Dice simulation range DP
Part 67: conversion between keypoint and point2f in opencv
Nacos offline service times error errcode: 500
SIGIR '22 recommendation system paper graph network