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

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

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

clipboard.png

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

Example 1:

Input:[  [0,0,0],  [0,1,0],  [0,0,0]]Output: 2Explanation:There is one obstacle in the middle of the 3x3 grid above.There are two ways to reach the bottom-right corner:1. Right -> Right -> Down -> Down2. Down -> Down -> Right -> Right

难度:medium

题目:在m * n 的格子左上角放置一个机器人,机器人在任何时候只能向右或向下移动,机器人尝试移动到格子的最右下角。如果在格子中加入障碍物。有多少种可能的走法?

思路:动态规划。将障碍点设置为0。

Runtime: 0 ms, faster than 100.00% of Java online submissions for Unique Paths II.

Memory Usage: 28.5 MB, less than 0.97% of Java online submissions for Unique Paths II.

class Solution {    public int uniquePathsWithObstacles(int[][] obstacleGrid) {        if (obstacleGrid[0][0] > 0) {            return 0;        }        int m = obstacleGrid.length;        int n = obstacleGrid[0].length;        obstacleGrid[0][0] = 1;        for (int i = 1; i < n; i++) {            obstacleGrid[0][i] = obstacleGrid[0][i] > 0 ? 0 : obstacleGrid[0][i - 1];        }        for (int i = 1; i < m; i++) {            obstacleGrid[i][0] = obstacleGrid[i][0] > 0 ? 0 : obstacleGrid[i - 1][0];        }        for (int i = 1; i < m; i++) {            for (int j = 1; j < n; j++) {                obstacleGrid[i][j] = obstacleGrid[i][j] > 0 ? 0 : obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];            }        }                return obstacleGrid[m - 1][n - 1];    }}

转载地址:http://ozvpa.baihongyu.com/

你可能感兴趣的文章
百世汇通快递地区选择插件,单独剥离
查看>>
Linux系统调用---同步IO: sync、fsync与fdatasync【转】
查看>>
【MyBatis学习06】输入映射和输出映射
查看>>
[LeetCode] Decode String 解码字符串
查看>>
数字逻辑的一些基本运算和概念
查看>>
ant重新编译打包hadoop-core-1.2.1.jar时遇到的错
查看>>
【★★★★★】提高PHP代码质量的36个技巧
查看>>
3 weekend110的配置hadoop(格式化) + 一些问题解决 + 未免密码配置
查看>>
JavaScript Creating 对象
查看>>
Java compiler level does not match the version of the installed Java project facet.(转)
查看>>
WPF MediaElement.Position属性
查看>>
sqoop数据迁移(基于Hadoop和关系数据库服务器之间传送数据)
查看>>
spring mysql多数据源配置
查看>>
[React] Override webpack config for create-react-app without ejection
查看>>
检索 COM 类工厂中 CLSID 为{00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。...
查看>>
测试java的父子类化
查看>>
HDOJ 1008
查看>>
安装thrift出现的一些问题
查看>>
makefile编写---单个子目录编译模板
查看>>
Oracle DB_LINK如何使用
查看>>