当前位置:网站首页>leetcode刷题:动态规划05(不同路径 II)
leetcode刷题:动态规划05(不同路径 II)
2022-07-23 14:58:00 【涛涛英语学不进去】
63. 不同路径 II
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

网格中的障碍物和空位置分别用 1 和 0 来表示。
示例 1:

- 输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
- 输出:2
解释: - 3x3 网格的正中间有一个障碍物。
- 从左上角到右下角一共有 2 条不同的路径:
- 向右 -> 向右 -> 向下 -> 向下
- 向下 -> 向下 -> 向右 -> 向右
示例 2:

- 输入:obstacleGrid = [[0,1],[0,0]]
- 输出:1
提示:
- m == obstacleGrid.length
- n == obstacleGrid[i].length
- 1 <= m, n <= 100
- obstacleGrid[i][j] 为 0 或 1
这题难在障碍物上,可能不止一个障碍物。
障碍物如果是起点,就直接返回0.
如果第一行或者第一列有障碍物,则障碍物之后的路线均为0
package com.programmercarl.dynamic;
import com.programmercarl.util.GenerateArray;
/** * @ClassName UniquePathsWithObstacles * @Descriotion TODO * @Author nitaotao * @Date 2022/7/23 9:35 * @Version 1.0 * https://leetcode.cn/problems/unique-paths-ii/ * 63. 不同路径 II **/
public class UniquePathsWithObstacles {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
/** * dp[ i ] 到达本位置有多少种走法 */
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
//如果起点是障碍物
if (obstacleGrid[0][0] == 1) {
return 0;
}
if (m == 1 && n == 1) {
if (obstacleGrid[0][0] == 0) {
return 1;
} else {
return 0;
}
}
int[][] dp = new int[m][n];
//第一行初始化
if (n != 1) {
if (obstacleGrid[0][1] == 0) {
dp[0][1] = 1;
}
for (int i = 2; i < n; i++) {
if (obstacleGrid[0][i] == 0) {
dp[0][i] = dp[0][i - 1];
} else {
//如果这一位是障碍物
dp[0][i] = 0;
}
}
}
//第一列初始化
if (m != 1) {
//如果第一位不是障碍物
if (obstacleGrid[1][0] == 0) {
dp[1][0] = 1;
}
for (int i = 2; i < m; i++) {
if (obstacleGrid[i][0] == 0) {
dp[i][0] = dp[i - 1][0];
} else {
//如果这一位是障碍物
dp[i][0] = 0;
}
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
//障碍物
if (obstacleGrid[i][j] == 1) {
dp[i][j] = 0;
continue;
}
//每个单元格的位置是左边格子或者上边格子移动过来的
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[obstacleGrid.length - 1][obstacleGrid[0].length - 1];
}
public static void main(String[] args) {
int[][] arr = new int[2][1];
arr[0] = new int[]{
0};
arr[1] = new int[]{
0};
System.out.println(new UniquePathsWithObstacles().uniquePathsWithObstacles(arr));
}
}

边栏推荐
- 来自某学生的求助,干了,闲暇能帮就帮一把!
- Kubernetes kubelet manages pod core process
- 林志颖仍在重症室 将进行二轮手术:警方称其并未系安全带
- Interviewer: what is the possible reason for the slow query of MySQL database besides the index problem?
- 工業物聯網中的時序數據
- How many common SQL misuses are there in MySQL?
- Description and usage of Axi interconnect IP core
- js工具 cecp
- sns_sensor_instance_api
- Deeply understand the mode and vibration of mechanical system
猜你喜欢

Log slimming operation: from 5g to 1g!

新零售电商平台怎么做?才能实现传统零售企业数字化转型?

Kv260 single board PS control setting IIC switch chip

程序环境和预处理
![[operation] Yan Yi (Internet new technology operation)](/img/7a/38c7a9ed79e626506de067f360384c.png)
[operation] Yan Yi (Internet new technology operation)

Kubernetes kubelet 硬核知识 架构

"Now, the number of codes has expanded to astronomical level"

用pymysql封装项目通用的连接和查询

可视化机房管理

Pymoo学习 (4): 多标准决策
随机推荐
Food safety | ham sausage lunch meat, is it really so unbearable?
Pymoo learning (3): use multi-objective optimization to find the set of optimal solutions
Kubernetes Kubelet管理pod核心流程
How to refine the operation of small program mall?
Thoughts on software quality system
Software configuration | Anaconda download, installation, environment configuration and uninstall
Description and usage of Axi interconnect IP core
From Markov chain to GPT, Li Hang, director of ByteDance AI Lab, detailed the past and present lives of language models
程序环境和预处理
[ pytorch ] 基本使用丨7. GPU分配丨
Can debug/release versions of dynamic library *.dll files be mixed (cross used)?
Kubernetes 聚焦Kubelet职责
SQL報錯盲注詳解
Food safety | what is the origin of plant meat that sounds very healthy?
常见模拟电路设计 一(含仿真):方波、三角波、正弦波的互相发生「建议收藏」
【Js】检查Date对象是否为Invalid Date
[MySQL Cluster fault recovery]
xlinx pcie xvc
Failure analysis and solution of vscode PIO creation project
Time series data in industrial Internet of things