Rotate image
1. Introduction
Given an n × n two-dimensional matrix represents an image.
Rotate the image 90 degrees clockwise.
Description:
You have to rotate the image in place, which means you need to directly modify the input two-dimensional matrix. Please do not use another matrix to rotate the image.
(Subject source: LeetCode )
示例 1:
给定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
原地旋转输入矩阵,使其变为:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
示例 2:
给定 matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
原地旋转输入矩阵,使其变为:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
Two, the solution
1. Use another matrix (the question asks not to do so, we will do it first, and gain experience in the process)
class Solution {
public static void rotate(int[][] matrix) {
//辅助矩阵
int matrixR[][]=new int[matrix.length][matrix.length];
//循环赋值
for (int i = 0; i < matrixR.length; i++) {
for (int j = 0; j < matrixR.length; j++) {
matrixR[j][matrixR.length-i-1]=matrix[i][j];
}
}
//将原数组替换,因为力扣不让返回,那只好改变原数组了
for (int i = 0; i < matrixR.length; i++) {
matrix[i]=matrixR[i];
}
}
}
2. Circularly move the
4 4 matrix:
we can see it as two squares, the outermost 4 4 square, and the inner 2*2 square.
First move the big square clockwise.
- Copy array [1,2,3,4] reserved
- Replace the first line with 1, 5, 9, 12
1 2 3 4 1 5 9 12
5 6 7 8 ===》 5 6 7 8
9 10 11 12 9 10 11 12
12 14 15 16 12 14 15 16
- Replace the first column with 12, 14, 15, 16
1 2 3 4 12 5 9 12
5 6 7 8 ===》 14 6 7 8
9 10 11 12 15 10 11 12
12 14 15 16 16 14 15 16
- Replace the fourth line with 16, 12, 8, 4
1 2 3 4 12 5 9 12
5 6 7 8 ===》 14 6 7 8
9 10 11 12 15 10 11 12
12 14 15 16 16 12 8 4
- Replace the copied array to the fourth column
1 2 3 4 12 5 9 1
5 6 7 8 ===》 14 6 7 2
9 10 11 12 15 10 11 3
12 14 15 16 16 12 8 4
The first square is rotated, and the second square has the same
code:
package com.lxf.test;
import java.util.Arrays;
public class Rotate {
public static void main(String[] args) {
int[][] arr=new int[][]{
{
5, 1, 9,11,17,18},
{
2, 4, 8,10,19,20},
{
13, 3, 6, 7,21,22},
{
15,14,12,16,23,24},
{
25,26,27,28,29,30},
{
31,32,33,34,35,36}
};
rotate(arr);
for (int[] nums : arr) {
for (int num : nums) {
System.out.print(num+" ");
}
System.out.println();
}
}
public static void rotate(int[][] matrix) {
int length=matrix.length;
for (int i = 0, j=length-1; i < length; i++,j--) {
//结束条件
if(i>=j){
break;
}
//记录第i个正方形的第一条边
int[] records = Arrays.copyOfRange(matrix[i], i,j+1);
//移动第四条边到第一条边
int k=j;
while(k>=i){
matrix[i][k]=matrix[length-1-k][i];
k--;
}
//移动第三条边到第四条边
int m=i;
while(m<=j){
matrix[m][i]=matrix[j][m];
m++;
}
//移动第二条边到第三条边
int n=j;
while(n>=i){
matrix[j][length-1-n]=matrix[n][j];
n--;
}
//将第一条边移动到第二条边
int p=i;
int q=0;
while (p<=j){
matrix[p][j]=records[q++];
p++;
}
}
}
}
Code optimization:
package com.lxf.test;
import java.util.Arrays;
public class Rotate {
public static void main(String[] args) {
int[][] arr=new int[][]{
{
5, 1, 9,11,17,18},
{
2, 4, 8,10,19,20},
{
13, 3, 6, 7,21,22},
{
15,14,12,16,23,24},
{
25,26,27,28,29,30},
{
31,32,33,34,35,36}
};
rotate(arr);
for (int[] nums : arr) {
for (int num : nums) {
System.out.print(num+" ");
}
System.out.println();
}
// 15,13,2,5
// 14,3,4,1
// 12,6,8,9
// 16,7,10,11
}
public static void rotate(int[][] matrix) {
int length=matrix.length;
for (int i = 0, j=length-1; i < length; i++,j--) {
//中止条件
if(i>=j){
break;
}
//记录正方形第一行
int[] records = Arrays.copyOfRange(matrix[i], i,j+1);
int k=j;
int one=matrix[j][i];
while(k>=i){
matrix[i][k]=matrix[length-1-k][i];
matrix[length-k-1][i]=matrix[j][length-k-1];
matrix[j][length-1-k]=matrix[k][j];
k--;
}
//遍历数组
int p=i;
int q=0;
while (p<=j){
matrix[p][j]=records[q++];
p++;
}
//注意:每一次都有一个错误,就是顶点交换时matrix[i][i]是最后一次赋值
//可是它原本值matrix[i][j]在第一波就被赋值了。最后再次处理:
matrix[i][i]=one;
//还有就是第一波matrix[j][j]被赋值数组最后一个值会引起matrix[0][j]最后一次赋值时错误
//但是我们是顺序遍历数组,列增加所以避免了这个错误
}
}
}
- More solutions: link