当前位置:网站首页>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;
}
边栏推荐
- Where is the DP interface of desktop computer (what if the host has no DP interface)
- websocket心跳机制(保活机制)
- 酪氨酸脱羧酶丨Worthington粪链球菌酪氨酸脱羧酶的特征
- fastdfs工作原理(技术原理)
- [data mining engineer - written examination] Dahua shares in 2022
- [self] - brush questions BFS
- 电商数据模型设计
- 搭载新一代超安全蜂窝电池,思皓爱跑上市13.99万元起售
- Codeforces Round #474 (Div. 1 + Div. 2) - C, F
- 深度剖析集成学习Adaboost
猜你喜欢

多传感器融合定位(三)——惯性技术

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

Use pytoch to quickly train the network model

VirtualLab基础实验教程-8.傅里叶变换(1)

My second uncle is angry and swipes the screen all over the network. How can he cure my spiritual internal friction?

PowerCLi 批量添加esxi到vCenter

Worthington丨STEMxyme的分类和测定方法

Deep analysis of integrated learning xgboost

苹果官网正在更新维护 Apple Store,国行 iPhone 13 / Pro 等产品将最高优惠 600 元

脲酶丨Worthington杰克豆脲酶的特性及测定方案
随机推荐
经典双栈实现队列,注意遍历栈的判定条件修改。
Worthington核糖核酸酶B历史和化学性质说明
Js判断数据类型的4种⽅式
Where is the DP interface of desktop computer (what if the host has no DP interface)
My second uncle is angry and swipes the screen all over the network. How can he cure my spiritual internal friction?
添加构建依赖项报错
深度剖析集成学习Xgboost(续)
电脑不知卸载什么,打不开计算器无法编辑截图功能打不开txt文件等等解决方案之一
多传感器融合定位(三)——惯性技术
[self] - brush questions set
The classic dual stack implementation queue, pay attention to the modification of the judgment conditions of traversing the stack.
有效供应链管理的八大绩效分析指标(上)
Manufacturing steps of interactive slide screen in exhibition hall
Codeforces Round #810 (Div. 2) A - C
失败率高达80%,数字化转型如何正确完成战略规划?
控件 圆角描边 MaterialShapeDrawable
深度剖析集成学习Xgboost
pycharm新建项目
SQL left connection, internal connection writing method "recommended collection"
Worthington丨STEMxyme的分类和测定方法