宏观分层
- 习题1 给定一个正方形矩阵matrix,原地调整成顺时针90度转动的样子
- 习题2 给定一个长方形矩阵matrix,实现转圈打印
- 习题3 给定一个正方形或者长方形矩阵matrix,实现zigzag打印
- 习题4 输入N,在控制台上输出初始边长为N的图案
习题1 给定一个正方形矩阵matrix,原地调整成顺时针90度转动的样子
a b c g d a
d e f h e b
g h i i f c
public static void spiralOrderPrint(int[][] matrix) {
int tR = 0;
int tC = 0;
int dR = matrix.length - 1;
int dC = matrix[0].length - 1;
while (tR <= dR && tC <= dC) {
printEdge(matrix, tR++, tC++, dR--, dC--);
}
}
public static void printEdge(int[][] m, int tR, int tC, int dR, int dC) {
if (tR == dR) {
for (int i = tC; i <= dC; i++) {
System.out.print(m[tR][i] + " ");
}
} else if (tC == dC) {
for (int i = tR; i <= dR; i++) {
System.out.print(m[i][tC] + " ");
}
} else {
int curC = tC;
int curR = tR;
while (curC != dC) {
System.out.print(m[tR][curC] + " ");
curC++;
}
while (curR != dR) {
System.out.print(m[curR][dC] + " ");
curR++;
}
while (curC != tC) {
System.out.print(m[dR][curC] + " ");
curC--;
}
while (curR != tR) {
System.out.print(m[curR][tC] + " ");
curR--;
}
}
}
public static void main(String[] args) {
int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
spiralOrderPrint(matrix);
}
习题2 给定一个长方形矩阵matrix,实现转圈打印
a b c d
e f g h
i j k L
打印顺序:a b c d h L k j I e f g
public static void rotate(int[][] matrix) {
int a = 0;
int b = 0;
int c = matrix.length - 1;
int d = matrix[0].length - 1;
while (a < c) {
rotateEdge(matrix, a++, b++, c--, d--);
}
}
public static void rotateEdge(int[][] m, int a, int b, int c, int d) {
int tmp = 0;
for (int i = 0; i < d - b; i++) {
tmp = m[a][b + i];
m[a][b + i] = m[c - i][b];
m[c - i][b] = m[c][d - i];
m[c][d - i] = m[a + i][d];
m[a + i][d] = tmp;
}
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i != matrix.length; i++) {
for (int j = 0; j != matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
printMatrix(matrix);
rotate(matrix);
System.out.println("=========");
printMatrix(matrix);
}
习题3 给定一个正方形或者长方形矩阵matrix,实现zigzag打印
0 1 2
3 4 5
6 7 8
打印: 0 1 3 6 4 2 5 7 8
public static void printMatrixZigZag(int[][] matrix) {
int tR = 0;
int tC = 0;
int dR = 0;
int dC = 0;
int endR = matrix.length - 1;
int endC = matrix[0].length - 1;
boolean fromUp = false;
while (tR != endR + 1) {
printLevel(matrix, tR, tC, dR, dC, fromUp);
tR = tC == endC ? tR + 1 : tR;
tC = tC == endC ? tC : tC + 1;
dC = dR == endR ? dC + 1 : dC;
dR = dR == endR ? dR : dR + 1;
fromUp = !fromUp;
}
System.out.println();
}
public static void printLevel(int[][] m, int tR, int tC, int dR, int dC, boolean f) {
if (f) {
while (tR != dR + 1) {
System.out.print(m[tR++][tC--] + " ");
}
} else {
while (dR != tR - 1) {
System.out.print(m[dR--][dC++] + " ");
}
}
}
public static void main(String[] args) {
int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
printMatrixZigZag(matrix);
}
习题4 输入N,在控制台上输出初始边长为N的图案
public static void printStar(int N) {
int leftUp = 0;
int rightDown = N - 1;
char[][] m = new char[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
m[i][j] = ' ';
}
}
while (leftUp <= rightDown) {
set(m, leftUp, rightDown);
leftUp += 2;
rightDown -= 2;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
public static void set(char[][] m, int leftUp, int rightDown) {
for (int col = leftUp; col <= rightDown; col++) {
m[leftUp][col] = '*';
}
for (int row = leftUp + 1; row <= rightDown; row++) {
m[row][rightDown] = '*';
}
for (int col = rightDown - 1; col > leftUp; col--) {
m[rightDown][col] = '*';
}
for (int row = rightDown - 1; row > leftUp + 1; row--) {
m[row][leftUp + 1] = '*';
}
}
public static void main(String[] args) {
printStar(5);
}