★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-cqybpjvt-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
How many possible unique paths are there?微信
Above is a 7 x 3 grid. How many possible unique paths are there?spa
Note: m and n will be at most 100.code
Example 1:htm
Input: m = 3, n = 2 Output: 3 Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: 1. Right -> Right -> Down 2. Right -> Down -> Right 3. Down -> Right -> Right
Example 2:blog
Input: m = 7, n = 3 Output: 28
一個機器人位於一個 m x n 網格的左上角 (起始點在下圖中標記爲「Start」 )。get
機器人每次只能向下或者向右移動一步。機器人試圖達到網格的右下角(在下圖中標記爲「Finish」)。博客
問總共有多少條不一樣的路徑?
例如,上圖是一個7 x 3 的網格。有多少可能的路徑?
說明:m 和 n 的值均不超過 100。
示例 1:
輸入: m = 3, n = 2 輸出: 3 解釋: 從左上角開始,總共有 3 條路徑能夠到達右下角。 1. 向右 -> 向右 -> 向下 2. 向右 -> 向下 -> 向右 3. 向下 -> 向右 -> 向右
示例 2:
輸入: m = 7, n = 3 輸出: 28
8ms
1 class Solution { 2 func uniquePaths(_ m: Int, _ n: Int) -> Int { 3 if m == 0 || n == 0 { 4 return 0 5 } 6 var columns: [Int] = Array(repeating: 1, count: m) 7 for _ in 1..<n { // 逐行遍歷 8 for i in 1..<m { // 第一位, 始終只有1種(+0) 9 columns[i] += columns[i-1] 10 } 11 } 12 return columns.last! 13 } 14 }
8ms
1 class Solution { 2 func uniquePaths(_ m: Int, _ n: Int) -> Int { 3 var pathNums = Array(repeating: Array(repeating:0,count: n),count: m) 4 return _helper(&pathNums, m - 1, n - 1) 5 } 6 private func _helper(_ pathNums: inout [[Int]], _ m: Int, _ n: Int) -> Int { 7 if m < 0 || n < 0 { 8 return 0 9 } 10 if m == 0 || n == 0 { 11 return 1 12 } 13 14 if pathNums[m][n] != 0 { 15 return pathNums[m][n] 16 } 17 pathNums[m][n] = _helper(&pathNums, m - 1, n) + _helper(&pathNums, m, n - 1) 18 19 return pathNums[m][n] 20 } 21 }
12ms
1 class Solution { 2 func uniquePaths(_ m: Int, _ n: Int) -> Int { 3 if m == 0 || n == 0 { return 0 } 4 var count = Array(repeating: Array(repeating: 0, count: n), count: m) 5 var col = n-1 6 while col >= 0 { 7 var row = m-1 8 while row >= 0 { 9 if row == m-1 && col == n-1 { 10 count[row][col] = 1 11 } else { 12 count[row][col] = (col < n-1 ? count[row][col+1] : 0) + (row < m-1 ? count[row+1][col] : 0) 13 } 14 row -= 1 15 } 16 col -= 1 17 } 18 return count[0][0] 19 } 20 }
16ms
1 class Solution { 2 3 func uniquePaths(_ m: Int, _ n: Int) -> Int { 4 guard m > 0 else { 5 return 0 6 } 7 8 var steps = Array(repeating: Array(repeating: 1, count: n), count: m) 9 for row in 1..<m { 10 for column in 1..<n { 11 steps[row][column] = steps[row - 1][column] + steps[row][column - 1] 12 } 13 } 14 15 return steps[m - 1][n - 1] 16 } 17 18 }
20ms
1 class Solution { 2 func uniquePaths(_ m: Int, _ n: Int) -> Int { 3 if m == 0 || n == 0 { 4 return 0 5 } 6 var map = Array(repeating: Array(repeating:0,count: n),count: m) 7 for i in 1 ..< m { 8 for j in 1 ..< n { 9 var num1 = map[i - 1][j] 10 var num2 = map[i][j - 1] 11 if i - 1 == 0 || j == 0 { 12 num1 = 1 13 } 14 if i == 0 || j - 1 == 0 { 15 num2 = 1 16 } 17 map[i][j] = num1 + num2 18 } 19 } 20 if m - 1 == 0 || n - 1 == 0 { 21 return 1 22 } else { 23 let result = map[m - 1][n - 1] 24 return result 25 } 26 } 27 }
24ms
1 class Solution { 2 3 func uniquePaths(_ m: Int, _ n: Int) -> Int { 4 var arrs: [[Int]] = Array(repeating: Array(repeating: 0, count: n), count: m) 5 for i in 0..<m{ 6 for j in 0..<n{ 7 if i == 0 && j == 0{ 8 arrs[i][j] = 1 9 }else if i == 0 { 10 arrs[i][j] = arrs[i][j-1] 11 }else if j == 0{ 12 arrs[i][j] = arrs[i-1][j] 13 }else{ 14 arrs[i][j] = arrs[i-1][j] + arrs[i][j-1] 15 } 16 } 17 } 18 return arrs[m-1][n-1] 19 } 20 }
32ms
1 class Solution { 2 func uniquePaths(_ m: Int, _ n: Int) -> Int { 3 guard m > 1 else { 4 return m == 0 ? 0 : 1 5 } 6 guard n > 1 else { 7 return n == 0 ? 0 : 1 8 } 9 10 var path = [Int].init(repeating: 1, count: m+1) 11 path[0] = 0 12 for _ in 2...n { 13 for j in 1...m { 14 path[j] = path[j] + path[j-1] 15 } 16 } 17 print(path) 18 return path[m] 19 } 20 }
40ms
1 class Solution { 2 func uniquePaths(_ m: Int, _ n: Int) -> Int { 3 let l = max(m,n) 4 let s = min(m,n) 5 if s==1 { 6 return 1 7 } 8 // c l,s 組合 9 var ji1 = 1 10 var ji2 = 1 11 for i in 1...s-1 { 12 ji1*=i 13 ji2*=l+s-i-1 14 } 15 return ji2/ji1 16 } 17 }