classSolution { public: intuniquePaths(int m, int n){ vector<vector<int>> dp(m, vector<int>(n, 0)); for (int i = 0; i < m; ++i) { dp[i][0] = 1; } for (int j = 0; j < n; ++j) { dp[0][j] = 1; } for (int i = 1; i < m; ++i) { for (int j = 1; j < n; ++j) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } return dp[m-1][n-1]; } };
结果
执行用时 : 0 ms, 击败 100.00% 使用 C++ 的用户
内存消耗 : 7.57 MB, 击败 12.32% 使用 C++ 的用户
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { publicintuniquePaths(int m, int n) { int[][] dp = newint[m][n]; for (inti=0; i < m; i++) { dp[i][0] = 1; } for (intj=0; j < n; j++) { dp[0][j] = 1; } for (inti=1; i < m; i++) { for (intj=1; j < n; j++) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } return dp[m - 1][n - 1]; } }
结果
执行用时 : 0 ms, 击败 100.00% 使用 Java 的用户
内存消耗 : 39.46 MB, 击败 21.94% 使用 Java 的用户
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution(object): defuniquePaths(self, m, n): """ :type m: int :type n: int :rtype: int """ dp = [[0] * n for _ inrange(m)] for i inrange(m): dp[i][0] = 1 for j inrange(n): dp[0][j] = 1 for i inrange(1, m): for j inrange(1, n): dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[m-1][n-1]
结果
执行用时 : 18 ms, 击败 44.36% 使用 Python 的用户
内存消耗 : 11.58 MB, 击败 67.39% 使用 Python 的用户
Python3
1 2 3 4 5 6 7 8 9 10 11
classSolution: defuniquePaths(self, m: int, n: int) -> int: dp = [[0] * n for _ inrange(m)] for i inrange(m): dp[i][0] = 1 for j inrange(n): dp[0][j] = 1 for i inrange(1, m): for j inrange(1, n): dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[m-1][n-1]
intuniquePaths(int m, int n) { int** dp = (int**)malloc(m * sizeof(int*)); for (int i = 0; i < m; ++i) { dp[i] = (int*)malloc(n * sizeof(int)); } for (int i = 0; i < m; ++i) { dp[i][0] = 1; } for (int j = 0; j < n; ++j) { dp[0][j] = 1; } for (int i = 1; i < m; ++i) { for (int j = 1; j < n; ++j) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } int result = dp[m-1][n-1]; for (int i = 0; i < m; ++i) { free(dp[i]); } free(dp); return result; }
结果
执行用时 : 0 ms, 击败 100.00% 使用 C 的用户
内存消耗 : 5.87 MB, 击败 62.56% 使用 C 的用户
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassSolution { publicintUniquePaths(int m, int n) { int[,] dp = newint[m, n]; for (int i = 0; i < m; i++) { dp[i, 0] = 1; } for (int j = 0; j < n; j++) { dp[0, j] = 1; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i, j] = dp[i - 1, j] + dp[i, j - 1]; } } return dp[m - 1, n - 1]; } }
classSolution { funcuniquePaths(_m: Int, _n: Int) -> Int { var dp =Array(repeating: Array(repeating: 0, count: n), count: m) for i in0..<m { dp[i][0] =1 } for j in0..<n { dp[0][j] =1 } for i in1..<m { for j in1..<n { dp[i][j] = dp[i -1][j] + dp[i][j -1] } } return dp[m -1][n -1] } }
结果
执行用时 : 2 ms, 击败 55.56% 使用 Swift 的用户
内存消耗 : 15.24 MB, 击败 6.17% 使用 Swift 的用户
Kotlin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { fununiquePaths(m: Int, n: Int): Int { val dp = Array(m) { IntArray(n) { 0 } } for (i in0 until m) { dp[i][0] = 1 } for (j in0 until n) { dp[0][j] = 1 } for (i in1 until m) { for (j in1 until n) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1] } } return dp[m - 1][n - 1] } }
结果
执行用时 : 140 ms, 击败 27.59% 使用 Kotlin 的用户
内存消耗 : 32.59 MB, 击败 34.48% 使用 Kotlin 的用户
Dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution{ int uniquePaths(int m, int n) { List<List<int>> dp = List.generate(m, (index) => List<int>.filled(n, 0)); for (int i = 0; i < m; i++) { dp[i][0] = 1; } for (int j = 0; j < n; j++) { dp[0][j] = 1; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } return dp[m - 1][n - 1]; } }
结果
执行用时 : 286 ms, 击败 100.00% 使用 Dart 的用户
内存消耗 : 146.46 MB, 击败 100.00% 使用 Dart 的用户
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
funcuniquePaths(m int, n int)int { dp := make([][]int, m) for i := 0; i < m; i++ { dp[i] = make([]int, n) } for i := 0; i < m; i++ { dp[i][0] = 1 } for j := 0; j < n; j++ { dp[0][j] = 1 } for i := 1; i < m; i++ { for j := 1; j < n; j++ { dp[i][j] = dp[i-1][j] + dp[i][j-1] } } return dp[m-1][n-1] }
结果
执行用时 : 0 ms, 击败 100.00% 使用 Go 的用户
内存消耗 : 1.95 MB, 击败 26.78% 使用 Go 的用户
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# @param {Integer} m # @param {Integer} n # @return {Integer} defunique_paths(m, n) dp = Array.new(m) { Array.new(n, 0) } (0...m).each { |i| dp[i][0] = 1 } (0...n).each { |j| dp[0][j] = 1 } (1...m).each do |i| (1...n).each do |j| dp[i][j] = dp[i - 1][j] + dp[i][j - 1] end end dp[m - 1][n - 1] end
结果
执行用时 : 69 ms, 击败 -% 使用 Ruby 的用户
内存消耗 : 206.30 MB, 击败 -% 使用 Ruby 的用户
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
objectSolution{ defuniquePaths(m: Int, n: Int): Int = { val dp = Array.ofDim[Int](m, n) for (i <- 0 until m) { dp(i)(0) = 1 } for (j <- 0 until n) { dp(0)(j) = 1 } for (i <- 1 until m) { for (j <- 1 until n) { dp(i)(j) = dp(i - 1)(j) + dp(i)(j - 1) } } dp(m - 1)(n - 1) } }