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,1,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
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
网格中的障碍物和空位置分别用 1
和 0
来表示。
说明:m 和 n 的值均不超过 100。
示例 1:
输入:[ [0,0]]输出: 2解释:3x3 网格的正中间有一个障碍物。从左上角到右下角一共有 条不同的路径:1. 向右 -> 向右 -> 向下 -> 向下2. 向下 -> 向下 -> 向右 -> 向右2
12ms
1 class Solution { 2 func uniquePathsWithObstacles(_ obstacleGrID: [[Int]]) -> Int { 3 let rowCount = obstacleGrID.count 4 let colCount = obstacleGrID.first?.count ?? 0 5 guard rowCount > 0,colCount > 0 else { return 0 } 6 var paths = Array(repeating: Array(repeating: 0,count: colCount),count: rowCount) 7 8 if obstacleGrID[0][0] != 1 { 9 paths[0][0] = 110 }11 for i in 1..<rowCount {12 if obstacleGrID[i][0] != 1 {13 paths[i][0] = paths[i-1][0]14 }15 }16 17 for i in 1..<colCount {18 if obstacleGrID[0][i] != 1 {19 paths[0][i] = paths[0][i-1]20 }21 }22 23 for r in 1..<rowCount {24 for c in 1..<colCount {25 if obstacleGrID[r][c] != 1 {26 paths[r][c] = paths[r-1][c] + paths[r][c-1]27 } else {28 paths[r][c] = 029 }30 }31 }32 33 return paths[rowCount-1][colCount-1]34 }35 }
16ms
1 class Solution { 2 func uniquePathsWithObstacles(_ obstacleGrID: [[Int]]) -> Int { 3 4 let m = obstacleGrID.count 5 if m == 0 { return 0 } 6 let n = obstacleGrID[0].count 7 if n == 0 { return 0 } 8 9 var f = [[Int]](repeating: [Int](repeating: 0,count: n),count: m)10 11 for i in 0..<m {12 if obstacleGrID[i][0] == 1 {13 break14 } else {15 f[i][0] = 116 }17 }18 19 for i in 0..<n {20 if obstacleGrID[0][i] == 1 {21 break22 } else {23 f[0][i] = 124 }25 }26 27 for i in 1..<m {28 for j in 1..<n {29 30 if obstacleGrID[i][j] == 1 {31 f[i][j] = 032 } else {33 f[i][j] = f[i - 1][j] + f[i][j - 1]34 }35 }36 }37 38 return f[m - 1][n - 1]39 }40 }
16ms
1 class Solution { 2 func uniquePathsWithObstacles(_ obstacleGrID: [[Int]]) -> Int { 3 guard obstacleGrID.count > 0 && obstacleGrID[0].count > 0 else { 4 return 0 5 } 6 var m = obstacleGrID.count,n = obstacleGrID[0].count 7 var dp = [[Int]](repeating: [Int](repeating: 0,count: m) 8 for i in 0..<m { 9 if obstacleGrID[i][0] == 1 {10 for k in i..<m {11 dp[k][0] = 012 }13 break14 } else {15 dp[i][0] = 116 }17 }18 for j in 0..<n {19 if obstacleGrID[0][j] == 1 {20 for k in j..<n {21 dp[0][k] = 022 }23 break24 } else {25 dp[0][j] = 126 }27 28 }29 30 for i in 1..<m {31 for j in 1..<n {32 if obstacleGrID[i][j] == 1 {33 dp[i][j] = 034 } else {35 dp[i][j] = dp[i - 1][j] + dp[i][j - 1]36 }37 38 }39 }40 41 return dp[m - 1][n - 1]42 }43 }
20ms
1 class Solution { 2 func uniquePathsWithObstacles(_ obstacleGrID: [[Int]]) -> Int { 3 guard obstacleGrID.count > 0 && obstacleGrID[0].count>0 else { 4 return 0 5 } 6 7 let rowCount = obstacleGrID.count 8 let colCount = obstacleGrID[0].count 9 var dp = [[Int]](repeating:[Int](repeating:1,count:colCount),count:rowCount)10 var occupIEdRow = false11 var occupIEdCol = false12 for row in 0..<rowCount {13 if obstacleGrID[row][0] == 1 || occupIEdRow {14 occupIEdRow = true15 dp[row][0] = 016 } 17 }18 19 for col in 0..<colCount {20 if obstacleGrID[0][col] == 1 || occupIEdCol {21 occupIEdCol = true22 dp[0][col] = 023 } 24 }25 26 for i in 1..<rowCount {27 for j in 1..<colCount{28 if obstacleGrID[i][j] == 1 {29 dp[i][j] = 030 } else {31 dp[i][j] = (obstacleGrID[i-1][j] == 1 ? 0 : dp[i-1][j]) + (obstacleGrID[i][j-1] == 1 ? 0 : dp[i][j-1])32 } 33 }34 }35 36 return dp[rowCount-1][colCount-1] 37 }38 }
24ms
1 class Solution { 2 func uniquePathsWithObstacles(_ obstacleGrID: [[Int]]) -> Int { 3 var Result = Array(repeating: Array(repeating: 0,count:obstacleGrID[0].count) 4 ,count: obstacleGrID.count) 5 if obstacleGrID.count == 0 || obstacleGrID[0].count == 0 || obstacleGrID[0][0] == 1 { 6 return 0 7 } 8 // 设置边界值,碰到障碍物后都无路径 9 for i in 0..<obstacleGrID.count {10 if obstacleGrID[i][0] == 1 {11 break12 }13 Result[i][0] = 114 }15 // 设置边界值,碰到障碍物后都无路径16 for j in 0..<obstacleGrID[0].count {17 if obstacleGrID[0][j] == 1 {18 break19 }20 Result[0][j] = 121 }22 23 // 动态规划求出路径(迭代法)24 for i in 1..<obstacleGrID.count {25 for j in 1..<obstacleGrID[0].count {26 if obstacleGrID[i][j] == 0 {27 Result[i][j] = Result[i-1][j] + Result[i][j-1]28 } else { // 障碍物无路劲29 Result[i][j] = 030 }31 }32 }33 return Result[Result.count - 1][Result[0].count - 1]34 }35 }
24ms
1 class Solution { 2 func uniquePathsWithObstacles(_ obstacleGrID: [[Int]]) -> Int { 3 let R = obstacleGrID.count 4 let C = obstacleGrID[0].count 5 var obstacleGrID = obstacleGrID 6 7 // If the starting cell has an obstacle,then simply return as there would be 8 // no paths to the destination. 9 if obstacleGrID[0][0] == 1 {10 return 011 }12 13 // Number of ways of reaching the starting cell = 1.14 obstacleGrID[0][0] = 115 16 // Filling the values for the first column17 for i in 1..<R {18 obstacleGrID[i][0] = (obstacleGrID[i][0] == 0 && obstacleGrID[i - 1][0] == 1) ? 1 : 019 }20 21 // Filling the values for the first row22 for i in 1..<C {23 obstacleGrID[0][i] = (obstacleGrID[0][i] == 0 && obstacleGrID[0][i - 1] == 1) ? 1 : 024 }25 26 // Starting from cell(1,1) fill up the values27 // No. of ways of reaching cell[i][j] = cell[i - 1][j] + cell[i][j - 1]28 // i.e. From above and left.29 for i in 1..<R {30 for j in 1..<C {31 if obstacleGrID[i][j] == 0 {32 obstacleGrID[i][j] = obstacleGrID[i - 1][j] + obstacleGrID[i][j - 1]33 } else {34 obstacleGrID[i][j] = 035 }36 }37 }38 39 // Return value stored in rightmost bottommost cell. That is the destination.40 return obstacleGrID[R - 1][C - 1]41 }42 }总结
以上是内存溢出为你收集整理的[Swift]LeetCode63. 不同路径 II | Unique Paths II全部内容,希望文章能够帮你解决[Swift]LeetCode63. 不同路径 II | Unique Paths II所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)