当前位置:网站首页>Leetcode63. 不同路径 II
Leetcode63. 不同路径 II
2022-07-28 22:06:00 【Java全栈研发大联盟】
题目传送地址:https://leetcode.cn/problems/unique-paths-ii/
运行效率:
代码如下:
public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length-1;
int n = obstacleGrid[0].length-1;
return uniquePaths(m, n, obstacleGrid);
}
public static int uniquePaths(int m, int n, int[][] obstacleGrid) {
HashMap<String, Integer> map = new HashMap<>();
return uniqPathsWithMap(m, n, obstacleGrid, map);
}
public static int uniqPathsWithMap(int m, int n, int[][] obstacleGrid, HashMap<String, Integer> map) {
if (obstacleGrid[m][n] == 1) {
return 0;
}
String key = m + ":" + n;
if (map.containsKey(key)) {
return map.get(key);
}
//处理边界情况
if (m == 0) {
while (n>0){
if(obstacleGrid[0][n-1]==1){
return 0;
}
n--;
}
return 1;
}
if(n==0){
while (m>0){
if(obstacleGrid[m-1][0]==1){
return 0;
}
m--;
}
return 1;
}
//要想走到(m,n)位置坐标,有2种可能,第一种是从(m-1,n)到(m,n), 第2种是从(m,n-1)到(m,n)
int i = uniqPathsWithMap(m - 1, n, obstacleGrid, map) + uniqPathsWithMap(m, n - 1, obstacleGrid, map);
map.put(m + ":" + n, i);
return i;
}
边栏推荐
猜你喜欢

Pagoda phpMyAdmin unauthorized access vulnerability

Uricase - Characteristics of uricase in Worthington pig liver:

2022 R2 mobile pressure vessel filling test question simulation test platform operation

多传感器融合定位(一)——3D激光里程计

What is a driver signature and how does the driver get a digital signature?

Deep analysis of integrated learning gbdt

Mongodb index add, view, export, delete

A new generation of ultra safe cellular battery, Sihao aipao, will be available from 139900 yuan

深度剖析集成学习GBDT

Apple's official website is being updated to maintain the apple store. Products such as the iPhone 13 / pro of the Bank of China will enjoy a maximum discount of 600 yuan
随机推荐
(22杭电多校二)Two Permutation (dp),Package Delivery (贪心)
器利而工善,以RPA+LCAP赋能企业司库管理数字化升级
Use pytoch to quickly train the network model
Multi sensor fusion positioning (II) -- map based positioning
智能垃圾桶(七)——SG90舵机的介绍与使用(树莓派pico实现)
简述分组密码的加密分组链接模式的工作原理及其特点(密码学移位密码加密解密)
Merkle tree
The classic dual stack implementation queue, pay attention to the modification of the judgment conditions of traversing the stack.
深度之眼(十八)——偏导数
猿人学第二十题
请简述list,set,map类型的集合的各自特点(简述三种不同的继承方式下)
What is a driver signature and how does the driver get a digital signature?
深度剖析集成学习Adaboost
深度剖析集成学习Xgboost
Compatibility description between kingbasees and Oracle (3. Common functions)
2022 G2 power plant boiler stoker examination question bank simulated examination platform operation
迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables
EN 1873屋面用装配附件.塑料单个屋面灯—CE认证
从XSS Payload学习浏览器解码
Jincang database kingbasees client programming interface guide ODBC (2. Overview)