题一:顺时针转圈打印矩阵
public void solve(int[][] matrix) {
int line = matrix.length;
int row = matrix[0].length;
//其实把for改成while看起来更干净些,下面的print里也是
//PS:row 英语原意是行 column是列
int startLine = 0, startRow = 0, endLine = line-1, endRow = row-1;
for (; startLine <= endLine && startRow <= endRow; startLine++,startRow++,endLine--,endRow--) {
print(matrix, startLine, startRow, endLine, endRow);
}
}
/**
*
* @param matrix 矩阵
* @param startLine 左上角开始节点的行
* @param startRow 左上角开始节点的列
* @param endLine 右下角结束节点的行
* @param endRow 右下角结束节点的列
*/
private void print(int[][] matrix, int startLine, int startRow, int endLine, int endRow) {
int i;
if (startLine == endLine) {
for (i = startRow; i <= endRow; i++) {
System.out.print(matrix[startLine][i] + " ");
}
return;
}
if (startRow == endRow) {
for (i = startLine; i <= endLine; i++) {
System.out.print(matrix[i][startRow] + " ");
}
return;
}
for (i = startRow; i <= endRow; i++) {
System.out.print(matrix[startLine][i] + " ");
}
for (i = startLine + 1; i <= endLine; i++) {
System.out.print(matrix[i][endRow] + " ");
}
for(i = endRow-1; i >= startRow; i--) {
System.out.print(matrix[endLine][i] + " ");
}
for (i = endLine-1; i > startLine; i--) {
System.out.print(matrix[i][startRow] + " ");
}
}
public static void main(String[] args) {
Page331 instance = new Page331();
int[][] matrix = {{1,2,3,4}, {10,11,12,5}, {9,8,7,6}};
instance.solve(matrix);
System.out.println();
int[][] matrix2 = {{1,2,3},{8,9,4},{7,6,5}};
instance.solve(matrix2);
System.out.println();
int[][] matrix3 = {{1,2,3,4,5},{18,19,20,21,6},{17,28,29,22,7},{16,27,30,23,8},{15,26,25,24,9},{14,13,12,11,10}};
instance.solve(matrix3);
System.out.println();
int[][] matrix4 = {{1},{2},{3},{4}};
instance.solve(matrix4);
System.out.println();
int[][] matrix5 = {{1,2,3,4,5}};
instance.solve(matrix5);
}
题二:“之“字形打印矩阵
"之"字形打印矩阵
两个指针:(sr,sc)先从第一行往右移动,到头就往下;(er,ec)第一列往下,到头往右
print方法里boolean控制打印方向,为true代表从下往上打印
注意 列比行长 与 行比列长 拐弯后不同的打印方向
public void solve(int[][] matrix) {
int rowL = matrix.length;
int columnL = matrix[0].length;
int sr = 0, sc = 0, er = 0, ec = 0;
while (sc < columnL && er < rowL) {
print(matrix, 0, sc, er, 0, (sc & 1) == 0);
sc++;
er++;
}
第一个指针沿着第一行到头
if (sc == columnL && er < rowL) {
while (er < rowL) {
print(matrix, ++sr, columnL-1, er, 0, (er & 1) == 0);
er++;
}
此时er == rowL,也就是需要拐弯向右
while (sr < rowL-1) {
print(matrix, ++sr, columnL-1, rowL-1, ++ec, (sr & 1) == 0);
}
}
第二个指针沿着第一列到头
else if (er == rowL && sc < columnL) {
while (sc < columnL) {
print(matrix, 0, sc, rowL-1, ++ec, (sc & 1) == 0);
sc++;
}
此时sc == columnL,需要拐弯向下
while (ec < columnL-1) {
print(matrix, ++sr, columnL-1, rowL-1, ++ec, (sr & 1) == 1);
}
}
方阵情况
else {
while (sr < rowL-1) {
print(matrix, ++sr, columnL-1, rowL-1, ++ec, (sr & 1) == 0);
}
}
}
orientation = true代表从下向上打印
private void print(int[][] matrix, int sr, int sc, int er, int ec, boolean orientation) {
if (orientation) {
while (er >= sr) {
System.out.print(matrix[er][ec] + " ");
er--;
ec++;
}
} else {
while (sr <= er) {
System.out.print(matrix[sr][sc] + " ");
sr++;
sc--;
}
}
}
public static void main(String[] args) {
Page335 instance = new Page335();
//列长
int[][] matrix = {{1,2,3,4}, {10,11,12,5}, {9,8,7,6}};
instance.solve(matrix);
//行长
System.out.println();
int[][] matrix3 = {{1,2,3,4,5},{18,19,20,21,6},{17,28,29,22,7},{16,27,30,23,8},{15,26,25,24,9},{14,13,12,11,10}};
instance.solve(matrix3);
System.out.println();
System.out.print("1 2 18 17 19 3 4 20 28 16 15 27 29 21 5 6 22 30 26 14 13 25 23 7 8 24 12 11 9 10" + "正确顺序");
//方阵
System.out.println();
int[][] matrix2 = {{1,2,3},{8,9,4},{7,6,5}};
instance.solve(matrix2);
//单列
System.out.println();
int[][] matrix4 = {{1},{2},{3},{4}};
instance.solve(matrix4);
//单行
System.out.println();
int[][] matrix5 = {{1,2,3,4,5}};
instance.solve(matrix5);
System.out.println();
int[][] matrix6 = {{1,2,3,4,5},{6,7,8,9,10}};
instance.solve(matrix6);
System.out.println();
int[][] matrix7 = {{1,2},{3,4},{5,6},{7,8}};
instance.solve(matrix7);
}