博客
关于我
leetcode 63. Unique Paths II
阅读量:134 次
发布时间:2019-02-26

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

一 题目

  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?

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级别,这个题目跟上一个差不多,区别就是这个加了障碍物。

所以解题思路一样。区别是加了障碍物的判断。

DFS

public static void main(String[] args) {		// TODO Auto-generated method stub		int[][] obstacleGrid ={				{0,0,0},				{0,1,0},				{0,0,0}		};	int res = uniquePathsWithObstacles( obstacleGrid);	System.out.println(res);		}    //dfs	public static int uniquePathsWithObstacles(int[][] obstacleGrid) {		int m = obstacleGrid.length;		int n = obstacleGrid[0].length;		 if (obstacleGrid[0][0] != 0 ||	                obstacleGrid[m - 1][n - 1] != 0) return 0;		int[][] f = new int[m][n];		f[0][0] = obstacleGrid[0][0]==0?1:0;		return dfs(m-1,n-1,f,obstacleGrid);				    }		static int dfs(int x, int y, int[][] f,int[][] obstacleGrid) {        if (x < 0 || y < 0) return 0; // 数据非法,终止条件             if(obstacleGrid[x][y]==1){//障碍物        	return 0;        }        if (x == 0 && y == 0) return f[0][0]; // 回到起点,收敛条件                if (f[x][y] > 0) {            return f[x][y];        } else {            return f[x][y] = dfs(x - 1, y,f,obstacleGrid) +  dfs(x, y - 1,f,obstacleGrid);        }    }

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

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

动态规划

  使用一个二维的 dp 数组,这里的 dp[i][j] 表示到达 (i, j) 位置的不同路径的数量,状态转移方程跟之前的一样:每个位置只能由其上面和左面的位置移动而来,也是由其上面和左边的 dp 值相加来更新当前的 dp 值,如下所示:

dp[i][j] = dp[i-1][j] + dp[i][j-1]

  另外,最上面的一行跟最左边的行初始化要判断下,不能全部为1,遇到障碍物就是0。

public static void main(String[] args) {		int[][] obstacleGrid ={				{0,0,0},				{0,1,0},				{0,0,0}		};	int res = uniquePathsWithObstacles( obstacleGrid);	System.out.println(res);		}		public static int uniquePathsWithObstacles(int[][] obstacleGrid) {		int m = obstacleGrid.length;		int n = obstacleGrid[0].length;				int[][] dp = new int[m][n];		//初始化第0列		for(int i=0;i

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

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

时间复杂度O(M*N).

 

 

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

你可能感兴趣的文章
MySQL创建用户报错:ERROR 1396 (HY000): Operation CREATE USER failed for 'slave'@'%'
查看>>
MySQL创建索引时提示“Specified key was too long; max key length is 767 bytes”
查看>>
mysql初始密码错误问题
查看>>
MySQL删除数据几种情况以及是否释放磁盘空间【转】
查看>>
Mysql删除重复数据通用SQL
查看>>
mysql判断某一张表是否存在的sql语句以及方法
查看>>
mysql加入安装策略_一键安装mysql5.7及密码策略修改方法
查看>>
mysql加强(1)~用户权限介绍、分别使用客户端工具和命令来创建用户和分配权限
查看>>
mysql加强(3)~分组(统计)查询
查看>>
mysql加强(4)~多表查询:笛卡尔积、消除笛卡尔积操作(等值、非等值连接),内连接(隐式连接、显示连接)、外连接、自连接
查看>>
mysql加强(5)~DML 增删改操作和 DQL 查询操作
查看>>
mysql加强(6)~子查询简单介绍、子查询分类
查看>>
mysql加强(7)~事务、事务并发、解决事务并发的方法
查看>>
MySQL千万级多表关联SQL语句调优
查看>>
mysql千万级大数据SQL查询优化
查看>>
MySQL千万级大表优化策略
查看>>
MySQL单实例或多实例启动脚本
查看>>
MySQL压缩包方式安装,傻瓜式教学
查看>>
MySQL原理、设计与应用全面解析
查看>>
MySQL原理简介—1.SQL的执行流程
查看>>