classSolution { public: voidrotate(vector<vector<int>>& matrix){ int n = matrix.size(); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { swap(matrix[i][j], matrix[j][i]); } } for (int i = 0; i < n; ++i) { reverse(matrix[i].begin(), matrix[i].end()); } } };
结果
执行用时 : 0 ms, 击败 100.00% 使用 C++ 的用户
内存消耗 : 8.52 MB, 击败 12.36% 使用 C++ 的用户
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { publicvoidrotate(int[][] matrix) { intn= matrix.length; for (inti=0; i < n; ++i) { for (intj= i + 1; j < n; ++j) { inttemp= matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for (inti=0; i < n; ++i) { for (intj=0; j < n / 2; ++j) { inttemp= matrix[i][j]; matrix[i][j] = matrix[i][n - 1 - j]; matrix[i][n - 1 - j] = temp; } } } }
结果
执行用时 : 0 ms, 击败 100.00% 使用 Java 的用户
内存消耗 : 41.23 MB, 击败 14.96% 使用 Java 的用户
Python
1 2 3 4 5 6 7 8 9 10 11 12
classSolution(object): defrotate(self, matrix): """ :type matrix: List[List[int]] :rtype: None Do not return anything, modify matrix in-place instead. """ n = len(matrix) for i inrange(n): for j inrange(i + 1, n): matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] for i inrange(n): matrix[i].reverse()
结果
执行用时 : 18 ms, 击败 39.91% 使用 Python 的用户
内存消耗 : 11.35 MB, 击败 98.01% 使用 Python 的用户
Python3
1 2 3 4 5 6 7 8 9 10 11
classSolution: defrotate(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ n = len(matrix) for i inrange(n): for j inrange(i + 1, n): matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] for i inrange(n): matrix[i].reverse()
结果
执行用时 : 35 ms, 击败 85.58% 使用 Python3 的用户
内存消耗 : 16.36 MB, 击败 46.87% 使用 Python3 的用户
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
voidrotate(int** matrix, int matrixSize, int* matrixColSize) { int n = matrixSize; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n / 2; ++j) { int temp = matrix[i][j]; matrix[i][j] = matrix[i][n - 1 - j]; matrix[i][n - 1 - j] = temp; } } }
结果
执行用时 : 0 ms, 击败 100.00% 使用 C 的用户
内存消耗 : 6.14 MB, 击败 84.61% 使用 C 的用户
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicclassSolution { publicvoidRotate(int[][] matrix) { int n = matrix.Length; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for (int i = 0; i < n; ++i) { Array.Reverse(matrix[i]); } } }
结果
执行用时 : 104 ms, 击败 73.95% 使用 C# 的用户
内存消耗 : 45.74 MB, 击败 6.72% 使用 C# 的用户
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * @param {number[][]} matrix * @return {void} Do not return anything, modify matrix in-place instead. */ var rotate = function(matrix) { const n = matrix.length; for (let i = 0; i < n; ++i) { for (let j = i + 1; j < n; ++j) { [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]]; } } for (let i = 0; i < n; ++i) { matrix[i].reverse(); } };
结果
执行用时 : 50 ms, 击败 94.94% 使用 JavaScript 的用户
内存消耗 : 49.44 MB, 击败 13.07% 使用 JavaScript 的用户
TypeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** Do not return anything, modify matrix in-place instead. */ functionrotate(matrix: number[][]): void { constn: number = matrix.length; for (let i = 0; i < n; ++i) { for (let j = i + 1; j < n; ++j) { [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]]; } } for (let i = 0; i < n; ++i) { matrix[i].reverse(); } }
classSolution { funcrotate(_matrix: inout [[Int]]) { let n = matrix.count for i in0..<n { for j in i+1..<n { (matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j]) } } for i in0..<n { matrix[i].reverse() } } }
结果
执行用时 : 6 ms, 击败 78.82% 使用 Swift 的用户
内存消耗 : 15.69 MB, 击败 5.88% 使用 Swift 的用户
Kotlin
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { funrotate(matrix: Array<IntArray>): Unit { val n = matrix.size for (i in0 until n) { for (j in i + 1 until n) { matrix[i][j] = matrix[j][i].also { matrix[j][i] = matrix[i][j] } } } for (i in0 until n) { matrix[i].reverse() } } }
结果
执行用时 : 206 ms, 击败 8.33% 使用 Kotlin 的用户
内存消耗 : 37.55 MB, 击败 6.25% 使用 Kotlin 的用户
Dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution{ void rotate(List<List<int>> matrix) { final n = matrix.length; for (var i = 0; i < n; ++i) { for (var j = i + 1; j < n; ++j) { final temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for (var i = 0; i < n; ++i) { matrix[i] = List<int>.from(matrix[i].reversed); } } }
结果
执行用时 : 299 ms, 击败 -% 使用 Dart 的用户
内存消耗 : 144.31 MB, 击败 100.00% 使用 Dart 的用户
Go
1 2 3 4 5 6 7 8 9 10 11 12 13
funcrotate(matrix [][]int) { n := len(matrix) for i := 0; i < n; i++ { for j := i + 1; j < n; j++ { matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] } } for i := 0; i < n; i++ { for j, k := 0, n-1; j < k; j, k = j+1, k-1 { matrix[i][j], matrix[i][k] = matrix[i][k], matrix[i][j] } } }
结果
执行用时 : 0 ms, 击败 100.00% 使用 Go 的用户
内存消耗 : 2.11 MB, 击败 16.92% 使用 Go 的用户
Ruby
1 2 3 4 5 6 7 8 9 10 11
# @param {Integer[][]} matrix # @return {Void} Do not return anything, modify matrix in-place instead. defrotate(matrix) n = matrix.length (0...n).each do |i| (i + 1...n).each do |j| matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] end end matrix.each { |row| row.reverse! } end
objectSolution{ defrotate(matrix: Array[Array[Int]]): Unit = { val n = matrix.length for (i <- 0 until n) { for (j <- i + 1 until n) { val temp = matrix(i)(j) matrix(i)(j) = matrix(j)(i) matrix(j)(i) = temp } } for (i <- 0 until n) { matrix(i) = matrix(i).reverse } } defmain(args: Array[String]): Unit = { val matrix = Array( Array(1, 2, 3), Array(4, 5, 6), Array(7, 8, 9) ) rotate(matrix) for (row <- matrix) { println(row.mkString(", ")) } } }