[Swift]LeetCode63. 不一樣路徑 II | Unique Paths II

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qiaadmsn-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).git

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).github

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.ide

Note: m and n will be at most 100.spa

Example 1:code

Input:
[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]
Output: 2
Explanation:
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 -> Down
2. Down -> Down -> Right -> Right

一個機器人位於一個 m x n 網格的左上角 (起始點在下圖中標記爲「Start」 )。htm

機器人每次只能向下或者向右移動一步。機器人試圖達到網格的右下角(在下圖中標記爲「Finish」)。blog

如今考慮網格中有障礙物。那麼從左上角到右下角將會有多少條不一樣的路徑?get

網格中的障礙物和空位置分別用 1 和 0 來表示。

說明:m 和 的值均不超過 100。

示例 1:

輸入:
[
  [0,0,0],
  [0,1,0],
  [0,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] = 1
10         }
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] = 0
29                 }
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                 break
14             } else {
15                 f[i][0] = 1
16             }
17         }
18         
19         for i in 0..<n {
20             if obstacleGrid[0][i] == 1 {
21                 break
22             } else {
23                 f[0][i] = 1
24             }
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] = 0
32                 } 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: n), count: m)
 8         for i in 0..<m {
 9             if obstacleGrid[i][0] == 1 {
10                 for k in i..<m {
11                     dp[k][0] = 0
12                 }
13                 break
14             } else {
15                 dp[i][0] = 1
16             }
17         }
18         for j in 0..<n {
19             if obstacleGrid[0][j] == 1 {
20                 for k in j..<n {
21                     dp[0][k] = 0
22                 }
23                 break
24             } else {
25                 dp[0][j] = 1
26             }
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] = 0
34                 } 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 = false
11         var occupiedCol = false
12         for row in 0..<rowCount {
13            if obstacleGrid[row][0] == 1 || occupiedRow {
14                occupiedRow = true
15                dp[row][0] = 0
16            } 
17         }
18             
19         for col in 0..<colCount {
20            if obstacleGrid[0][col] == 1 || occupiedCol {
21                occupiedCol = true
22                dp[0][col] = 0
23            } 
24         }
25         
26         for i in 1..<rowCount {
27             for j in 1..<colCount{
28                 if obstacleGrid[i][j] == 1 {
29                     dp[i][j] = 0
30                 } 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                  break
12              }
13              Result[i][0] = 1
14         }
15         // 設置邊界值,碰到障礙物後都無路徑
16         for j in 0..<obstacleGrid[0].count {
17             if obstacleGrid[0][j] == 1 {
18                 break
19             }
20             Result[0][j] = 1
21         }
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] = 0
30                 }
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 0
11         }
12 
13         // Number of ways of reaching the starting cell = 1.
14         obstacleGrid[0][0] = 1
15 
16         // Filling the values for the first column
17         for i in 1..<R {
18             obstacleGrid[i][0] = (obstacleGrid[i][0] == 0 && obstacleGrid[i - 1][0] == 1) ? 1 : 0
19         }
20 
21         // Filling the values for the first row
22         for i in 1..<C {
23             obstacleGrid[0][i] = (obstacleGrid[0][i] == 0 && obstacleGrid[0][i - 1] == 1) ? 1 : 0
24         }
25 
26         // Starting from cell(1,1) fill up the values
27         // 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] = 0
35                 }
36             }
37         }
38 
39         // Return value stored in rightmost bottommost cell. That is the destination.
40         return obstacleGrid[R - 1][C - 1]
41     }
42 }
相關文章
相關標籤/搜索