LeetCode 63 — Unique Paths II(C++ Java Python)

原创 2014年02月25日 22:19:29

题目:http://oj.leetcode.com/problems/unique-paths-ii/

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.

题目翻译:

"Unique Paths"续:
现在考虑如果在网格中添加一些障碍,会有多少种不同的路径?
网格中分别用1和0标记障碍物和空的网格。
例如,
下图所示的3 x 3方格的中间有一个障碍。
不同的路径总数为2。
注意:m和n最大为​​100。

分析:

        有障碍的地方路径数为0。

C++实现:

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
    	int m = obstacleGrid.size();
    	int n = obstacleGrid[0].size();

    	if(obstacleGrid[0][0] == 1)
    	{
    		return 0;
    	}
    	else if(m == 1 && n == 1)
    	{
    		return 1;
    	}

    	int paths[m][n];

    	for(int i = 0; i < m; ++i)
    	{
    		if(obstacleGrid[i][0] == 1)
    		{
    			while(i < m)
    			{
    				paths[i][0] = 0;
    				++i;
    			}
    			break;
    		}
    		else
    		{
    			paths[i][0] = 1;
    		}
    	}

    	for(int j = 1; j < n; ++j)
        {
        	if(obstacleGrid[0][j] == 1)
        	{
        		while(j < n)
        		{
        			paths[0][j] = 0;
        			++j;
        		}
        		break;
        	}
        	else
        	{
        		paths[0][j] = 1;
        	}
        }

    	for(int i = 1; i < m; ++i)
    		for(int j = 1; j < n; ++j)
    		{
    			if(obstacleGrid[i][j] == 1)
    			{
    				paths[i][j] = 0;
    			}
    			else
    			{
    				paths[i][j] = paths[i][j - 1] + paths[i - 1][j];
    			}
    		}

    	return paths[m - 1][n - 1];
    }
};

Java实现:

public class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
		int m = obstacleGrid.length;
		// if (m == 0) {
		// 	   return 0;
		// }

		int n = obstacleGrid[0].length;

		if (obstacleGrid[0][0] == 1) {
			return 0;
		} else if (m == 1 && n == 1) {
			return 1;
		}

		int[][] paths = new int[m][n];

		for (int i = 0; i < m; ++i) {
			if (obstacleGrid[i][0] == 1) {
				while (i < m) {
					paths[i][0] = 0;
					++i;
				}
				break;
			} else {
				paths[i][0] = 1;
			}
		}

		for (int j = 1; j < n; ++j) {
			if (obstacleGrid[0][j] == 1) {
				while (j < n) {
					paths[0][j] = 0;
					++j;
				}
				break;
			} else {
				paths[0][j] = 1;
			}
		}

		for (int i = 1; i < m; ++i)
			for (int j = 1; j < n; ++j) {
				if (obstacleGrid[i][j] == 1) {
					paths[i][j] = 0;
				} else {
					paths[i][j] = paths[i][j - 1] + paths[i - 1][j];
				}
			}

		return paths[m - 1][n - 1];
    }
}

Python实现:

class Solution:
    # @param obstacleGrid, a list of lists of integers
    # @return an integer
    def uniquePathsWithObstacles(self, obstacleGrid):
        m = len(obstacleGrid)
        n = len(obstacleGrid[0])
        
        if obstacleGrid[0][0] == 1:
            return 0
        elif m == 1 and n == 1:
            return 1
        
        paths = [[] for i in range(m)] 
        
        for i in range(m):
            if obstacleGrid[i][0] == 1:
                while(i < m):
                    paths[i].append(0)
                    i += 1
                break;
            else:
                paths[i].append(1)
        
        for j in range(1, n):
            if obstacleGrid[0][j] == 1:
                while(j < n):
                    paths[0].append(0)
                    j += 1
                break;
            else:
                paths[0].append(1)
                
        for i in range(1, m):
            for j in range(1, n):
                if obstacleGrid[i][j] == 1:
                    paths[i].append(0)
                else:
                    paths[i].append(paths[i][j - 1] + paths[i - 1][j])
                    
        return paths[m - 1][n - 1]

        感谢阅读,欢迎评论!

【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】

【062-Unique Paths(唯一路径)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  A robot is located at the top-left cor...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月01日 07:16
  • 1675

有障碍物的矩阵格路径的个数 Unique Paths II

在《矩形格路径的个数 Unique Paths》中,介绍了求解矩阵格路径个数的两种方法,这种属于没有阻碍的路径个数。本文要处理的问题是,如果人为的在矩阵的某些格子中布置障碍物,意味着不能通过该格子。那...
  • luckyjoy521
  • luckyjoy521
  • 2014年03月20日 13:14
  • 1255

【LeetCode-面试算法经典-Java实现】【063-Unique Paths II(唯一路径问题II)】

【063-Unique Paths II(唯一路径问题II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Follow up for “Unique Paths”: ...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月01日 07:19
  • 1778

【leetcode】【63】Unique Paths II

一、问题描述 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. ...
  • xiaoliucool1314
  • xiaoliucool1314
  • 2016年03月02日 09:46
  • 147

【leetcode】63. Unique Paths II

一、题目描述 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m...
  • xiaoquantouer
  • xiaoquantouer
  • 2016年10月23日 14:48
  • 143

LeetCode-63. Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many uni...
  • u012848330
  • u012848330
  • 2016年10月07日 15:30
  • 137

[LeetCode]--63. Unique Paths II

Follow up for “Unique Paths”:Now consider if some obstacles are added to the grids. How many unique ...
  • bug_moving
  • bug_moving
  • 2016年12月03日 17:06
  • 268

<LeetCode OJ> 62. / 63. Unique Paths(I / II)

63. Unique Paths II My Submissions Question Total Accepted: 55136 Total Submissions: 191949 Diff...
  • EbowTang
  • EbowTang
  • 2016年01月08日 22:05
  • 1950

Unique Paths II - LeetCode 63

题目描述: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How ma...
  • bu_min
  • bu_min
  • 2015年05月07日 21:04
  • 192

Leetcode NO.63 Unique Paths II

题目要求如下: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids....
  • u012290414
  • u012290414
  • 2015年02月07日 08:07
  • 335
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 63 — Unique Paths II(C++ Java Python)
举报原因:
原因补充:

(最多只允许输入30个字)