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

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

原题地址:https://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.

解题思路:这道题是设置了障碍的,也是用动态规划解决。

代码:

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

 

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

你可能感兴趣的文章
2012/10/8——假期结束了
查看>>
RIYUXX
查看>>
finalize和System.gc()的区别
查看>>
几个数组去重的方法
查看>>
微软指定推荐沃通WoSign EV代码签名证书
查看>>
架构师速成-如何高效编程 for java
查看>>
struts2上传文件
查看>>
敏捷软件开发宣言和敏捷宣言遵循的12个原则
查看>>
前端日志
查看>>
重复数据删除和SSD的互补方法
查看>>
Using Regular Expressions in Python
查看>>
MVC 3.0 学习1
查看>>
Cracking the coding interview--Q1.1
查看>>
Delphi中GDI+实现图像二值化
查看>>
类的声明和实现 oc
查看>>
Cent OS 6.5安装 php memcached扩展
查看>>
LeetCode-152-Maximum Product Subarray
查看>>
Scrapy 学习记录
查看>>
【LeetCode每天一题】Maximum Subarray(最大子数组)
查看>>
DLL文件编译出错_DllMain@12 already defined in XXX.obj
查看>>