标签:Rotate last matrix int Image top length rotate LeetCode
You are given an n x n 2D matrix
representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2:
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Example 3:
Input: matrix = [[1]] Output: [[1]]
Example 4:
Input: matrix = [[1,2],[3,4]] Output: [[3,1],[4,2]]
Constraints:
matrix.length == n
matrix[i].length == n
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000
Solution 1. O(n^2) runtime, O(n^2) space
1 public class Solution { 2 public void rotate(int[][] matrix) { 3 if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { 4 return; 5 } 6 int n = matrix.length; 7 int[][] rotate = new int[n][n]; 8 for(int i = 0; i < n; i++){ 9 for(int j = 0; j < n; j++){ 10 rotate[i][j] = matrix[n - 1 - j][i]; 11 } 12 } 13 for(int i = 0; i < n; i++){ 14 for(int j = 0; j < n; j++){ 15 matrix[i][j] = rotate[i][j]; 16 } 17 } 18 //matrix = rotate; 19 } 20 }
One pitfall here is that line 18 does not change the original matrix that needs to be rotated. Java is pass-by-copy, so a copy of matrix, which is
a reference to the actual memory location of the 2D matrix, is passed to method rotate. You should use this copy of reference to change the
actual matrix entries. Changing this copy of reference to a new location does not touch the original matrix at all.
Solution 2. O(n^2) runtime, O(1) space
The runtime in solution 1 is already BCR asymptotically. But we can still optimize on extra memory usage.
Perform a circular rotation on each layer,(left -> top, bottom -> left, right -> bottom, top -> bottom. ),
starting from the outermost layer and working our way inwards.
How do we perform this 4-way edge swap? One option is to copy the top edge to an array, then move
the left to the top, the bottom to the left, and so on. This requires O(n) memory.
A better way to do this is to implement the swap index by index, so we only needs O(1) extra memory.
1 public class Solution { 2 public void rotate(int[][] matrix) { 3 if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { 4 return; 5 } 6 int n = matrix.length; 7 for(int layer = 0; layer < n / 2; layer++){ 8 int first = layer; 9 int last = n - 1 - layer; 10 for(int i = first; i < last; i++){ 11 int offset = i - first; 12 //save top 13 int top = matrix[first][i]; 14 //left -> top 15 matrix[first][i] = matrix[last - offset][first]; 16 //bottom -> left 17 matrix[last - offset][first] = matrix[last][last - offset]; 18 //right -> bottom 19 matrix[last][last - offset] = matrix[i][last]; 20 //top -> right 21 matrix[i][last] = top; 22 } 23 } 24 } 25 }
Related Problems
Rotate String
Matrix Zigzag Traversal
Set Matrix Zeroes
标签:Rotate,last,matrix,int,Image,top,length,rotate,LeetCode 来源: https://www.cnblogs.com/lz87/p/7056900.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。