博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 63. Unique Paths II
阅读量:6992 次
发布时间:2019-06-27

本文共 1100 字,大约阅读时间需要 3 分钟。

 

 

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.

分析:

题中给出了m×n的方格,m和n最大值为100,左上角的小机器人只能每次向右走一步或者向下走一步,终点在方格的右下角,

求一共有多少条不同的路径。

最优的子结构: a[i,j] = a[i-1,j] + a[i,j-1]

第一行中,不管到达哪个点,都是沿着第一行一直走才能达到的,如果第一行的值为0,第一行的点的路径为1, a[i][0] = 1;

              如果遇到了障碍物,为1的后面的方格都走不到了,所以跳出循环。

第一列中,不管到达哪个点,都是沿着第一列一直走才能达到的,如果方格内的值为0,第一行的点的路径为1, a[0][j] = 1;

              如果遇到了障碍物,为1的后面的方格都走不到了,所以跳出循环。

 

从起点到方格内某个点的不同路径数量可以分解为该点上方的点和左侧的点路径之和。

 

 

public class Solution {    public int uniquePathsWithObstacles(int[][] obstacleGrid) {        if(obstacleGrid.length == 0 || obstacleGrid[0].length == 0){            return 0;        }        int n = obstacleGrid.length;        int m = obstacleGrid[0].length;        int[][] a = new int[n][m];        for(int i=0;i

 

转载于:https://www.cnblogs.com/iwangzheng/p/5784352.html

你可能感兴趣的文章
自定义值转换器
查看>>
数据库索引
查看>>
Windows Mobile开发资源站点集锦
查看>>
IT兄弟连 JavaWeb教程 Servlet表单数据
查看>>
剑法三套,程序员也能赚大钱(2) 转
查看>>
C# OpenFileDialog用法
查看>>
个人第一款小工具-批量文件重命名By Qt 5(Qt 5.2.1 + MSVC2012)
查看>>
《Java EE 开发技术与案例教程》 这是一本好书啊:简洁精辟(相见恨晚)
查看>>
十、装饰(Decorator)模式 --结构模式(Structural Pattern)
查看>>
WWDC 2013 Session笔记 - UIKit Dynamics入门
查看>>
5月7日——采用第三方页面内容,但是顶部title使用自己的
查看>>
RGBa颜色 css3的Alpha通道支持
查看>>
SSE图像算法优化系列十八:三次卷积插值的进一步SSE优化。
查看>>
unity SystemInfo类 获得电量battery
查看>>
[好文要转]【关于block使用的5点注意事项】
查看>>
Windows如何安装自定义服务
查看>>
095、如何创建Swarm集群?(Swarm02)
查看>>
结对开发地铁
查看>>
附加题
查看>>
this kernel requires an x86-64 cpu,but only detected an i686 cpu
查看>>