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.

跟上一个题类似。

题目大意:左上走到右下,1表示障碍,0表示路,问有多少种唯一的走法。

解题思路:简单的动规,F(i,j)=F(i-1,j)+F(i,j-1),如果grid[i][j]==1,那么F(i,j)=0。

public class Solution {
public int uniquePathsWithObstacles(int[][] og) {
if(og==null||og[0].length==0||og[0][0]==1){
return 0;
}
int m = og.length,n=og[0].length;
int[][] cnt = new int[m][n];
cnt[0][0]=1;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if((i==0&&j==0)||og[i][j]==1){
continue;
} else if(i==0&&j!=0){
cnt[i][j]=cnt[i][j-1];
} else if(j==0&&i!=0){
cnt[i][j]=cnt[i-1][j];
} else {
cnt[i][j]=cnt[i-1][j]+cnt[i][j-1];
}
}
}
return cnt[m-1][n-1];
}
}

Unique Paths II ——LeetCode的更多相关文章

  1. Unique Paths II [LeetCode]

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  2. Unique Paths II leetcode java

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  3. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  4. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  5. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  6. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  7. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  8. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

  9. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

随机推荐

  1. 11.1 afternoon

    幸运数字(number)Time Limit:1000ms Memory Limit:64MB题目描述LYK 最近运气很差,例如在 NOIP 初赛中仅仅考了 90 分,刚刚卡进复赛,于是它决定使用一些 ...

  2. HTML5+移动APP(1)

    前言: 介绍使用html5+(nativejs)和mui开发移动app(包括Android和iOs) HBuilder h5+开发app的环境,是一个对eclipse做了深度定的IDE. 官网: ht ...

  3. Gprinter Android SDK V2.1.4 使用说明

    佳博打印机Android的SDK开发包,已更新到Gprinter Android SDK V2.1.4. IOS的SDK开发包更新为GprinterSDKandDemoforIOS_v1.0.8. 根 ...

  4. Deep Learning 学习随记(五)深度网络--续

    前面记到了深度网络这一章.当时觉得练习应该挺简单的,用不了多少时间,结果训练时间真够长的...途中debug的时候还手贱的clear了一下,又得从头开始运行.不过最终还是调试成功了,sigh~ 前一篇 ...

  5. 【转】iOS25彩票 幸运转盘

    原文 : http://www.it165.net/pro/html/201409/21216.html 最终效果图: 各个view的关系图: 背景圆盘(需穴ky"http://www.it ...

  6. 【转】 KVC/KVO原理详解及编程指南

    原文地址:http://blog.csdn.net/wzzvictory/article/details/9674431 前言: 1.本文基本不讲KVC/KVO的用法,只结合网上的资料说说对这种技术的 ...

  7. 关于alarm函数

    #include<unistd.h> #include<signal.h> void handler() { printf("Hello\n"); sign ...

  8. Linux常用命令大全(2)

    系统信息arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS / ...

  9. 浏览器JS报错Uncaught RangeError: Maximum call stack size exceeded?

    JavaScript错误:Uncaught RangeError: Maximum call stack size exceeded 堆栈溢出 原因:有小类到大类的递归查询导致溢出 解决方法思想: A ...

  10. unix 环境高级编程 读书笔记与习题解答第四篇

    第一章 第六节 第一小节 这一章没有程序设计和API方面的深入学习,而是注重介绍了unix操作系统中的原始数据类型和系统原型函数,错误处理方面的知识. ____unistd.h____ 该文件包含了u ...