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

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

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.

public class Solution {    public int uniquePathsWithObstacles(int[][] obstacleGrid) {        int path = 0;        int row = obstacleGrid.length;        if(row > 0){        	int column = obstacleGrid[0].length;        	int[][] paths = new int[row][column];        	if(obstacleGrid[0][0] != 1){        		paths[0][0] = 1;        		for(int i = 1; i < row; ++i){        			if(obstacleGrid[i][0] != 1)        				paths[i][0] = paths[i - 1][0];        			else        				paths[i][0] = 0;        		}        		for(int i = 1; i < column; ++i){        			if(obstacleGrid[0][i] != 1)        				paths[0][i] = paths[0][i - 1];        			else        				paths[0][i] = 0;        		}        		        		for(int i = 1; i < row; ++i){        			for(int j = 1; j < column; ++j){        				if(obstacleGrid[i][j] != 1)            				paths[i][j] = paths[i - 1][j] + paths[i][j - 1];            			else            				paths[i][j] = 0;        			}        		}        		path = paths[row - 1][column - 1];        	}        }        return path;        }}

  

转载于:https://www.cnblogs.com/averillzheng/p/3774551.html

你可能感兴趣的文章
给网页标题添加小图标
查看>>
Effective Scala
查看>>
Tasks and jobs
查看>>
python小程序之一
查看>>
数据解析
查看>>
Spring Ioc原理
查看>>
关于深拷贝与浅拷贝的一些简单说明
查看>>
TCP三次握手和四次握手
查看>>
js 鼠标事件
查看>>
AnsiString用法(转)
查看>>
DP E - Cheapest Palindrome
查看>>
用TTL线在CFE环境下拯救半砖wrt54g路由器
查看>>
soundpool
查看>>
JEECG前后端分离UI框架实战版本抢先体验(ng2-admin+Angular4+AdminLTE+WebStorm)
查看>>
Web基础--JavaScript入门
查看>>
HTML5冲刺
查看>>
OpenJudge计算概论-完美立方【暂时就想到了枚举法了】
查看>>
A Knight's Journey
查看>>
.NET自我进阶以及第一个框架搭建(一)
查看>>
简单解决 ATL:CString WTL:CString 冲突
查看>>