[Leetcode] Rotate Matrix
2014-08-13 23:11
393 查看
Rotate a matrix for 90 degree in clockwise.
public void rotate(int[][] matrix) {
int n = matrix[0].length;
for (int i = 0; i < Math.floor(n/2) ; i++) {
for (int j = i; j < n-1-i; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n-1-j][i];
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
matrix[j][n-1-i] = temp;
}
}
}
This is how I did after stupid thinking for hours T^T.
But I think this's the most elegant solution which has the smallest time and space complex.
public void rotate(int[][] matrix) {
int n = matrix[0].length;
for (int i = 0; i < Math.floor(n/2) ; i++) {
for (int j = i; j < n-1-i; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n-1-j][i];
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
matrix[j][n-1-i] = temp;
}
}
}
This is how I did after stupid thinking for hours T^T.
But I think this's the most elegant solution which has the smallest time and space complex.
相关文章推荐
- [leetcode]Rotate Image, Matrix tranposition, matrix rotation 90 degree @ Python
- LeetCode - 67. Rotate Matrix
- [LeetCode] Spiral Matrix、Rotate Image、Spiral Matrix II、Set Matrix Zeroes、Search a 2D Matrix
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
- leetcode笔记:Longest Increasing Path in a Matrix
- LeetCode(73) Set Matrix Zeroes
- Leetcode 74. Search a 2D Matrix 2D矩阵查找 解题报告
- 第十八周:[Leetcode]74. Search a 2D Matrix
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
- LeetCode-378. Kth Smallest Element in a Sorted Matrix
- LeetCode:566. Reshape the Matrix
- LeetCode 48. Rotate Image(java)
- [LeetCode] Set Matrix Zeroes
- Leetcode 74. Search a 2D Matrix
- leetcode-Set Matrix Zeroes
- [LeetCode] 61. Rotate List 解题思路
- [leetcode] Search a 2D Matrix
- leetcode---Kth Smallest Element in a Sorted Matrix
- [leetcode]61. Rotate List@Java解题报告
- LeetCode 61. Rotate List