package com.changhong.epc.tenant; import org.junit.Test; import java.util.Arrays; public class MasterTest { @Test public void test(){ int[][] matrix = new int[][]{ {1, 2, 3} , {4, 5, 6} , {7, 8, 9} }; rotate(matrix); for(int[] o : matrix){ System.out.println(Arrays.toString(o)); } } public void rotate(int[][] matrix) { int layer = matrix.length / 2 , index = 0; if(layer < 1){ return; } do{ turn(matrix, index++); }while (layer > index); } int x = 0 , y = 0; int block=0; int length=0; public void turn(int[][] matrix, int layer){ x = y = layer; block = 0; length = matrix.length-1; do{ System.out.println(matrix[x][y]); next(); }while (x != -1); } public void next(){ switch (block){ case 0: if(y == length){ block++; next(); break; } y++; break; case 1: if(x == length){ block++; next(); break; } x++; break; case 2: if(y == 0){ block++; next(); break; } y--; break; case 3: if(x == 0){ x = y = -1; break; } x--; break; } } }