P127、面试题20:顺时针打印矩阵
题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。例如:如果输入如下矩阵:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
则依次打印出数字1、2、3、4、8、12、16、15、14、13、9、5、6、7、11、10。
测试用例:
数组有多行多列,数组只有一行,数组中只有一列,数组中只有一行一列
代码实现:
package com.yyq; /** * Created by Administrator on 2015/9/16. */ public class PrintMatrix { public static void printMatrixClockwisely(int[][] numbers, int columns, int rows){ if (numbers == null || columns < 0 || rows < 0){ return; } int start = 0; while(columns > start * 2 && rows > start * 2){ printMatrixInCircle(numbers,columns,rows,start); start ++; } } public static void printMatrixInCircle(int[][] numbers, int columns, int rows, int start){ int endX = columns - 1 - start; int endY = rows - 1 - start; //从左到右打印一行 for (int i = start; i <= endX; i++){ int number = numbers[start][i]; printNumber(number); } //从上到下打印一列 if (start < endY){ for (int i = start+1; i <= endY; i++){ int number = numbers[i][endX]; printNumber(number); } } //从右到左打印一行 if (start < endX && start < endY){ for (int i = endX - 1; i >= start; i--){ int number = numbers[endY][i]; printNumber(number); } } if (start < endX && start < endY - 1){ for (int i = endY - 1; i >= start+1; i--){ int number = numbers[i][start]; printNumber(number); } } } public static void printNumber(int number){ System.out.print(number+"\t"); } // ====================测试代码==================== public static void Test(String testName, int columns, int rows) { System.out.println(testName+" Begin: " + columns + " columns, " + rows + " rows."); if(columns < 1 || rows < 1) return; int[][] numbers = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { numbers[i][j] = i * columns + j + 1; } } printMatrixClockwisely(numbers, columns, rows); System.out.println(""); } public static void main(String[] args){ /* 1 */ Test("test1",1, 1); /* 1 2 3 4 */ Test("test2",2, 2); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 */ Test("test3",4, 4); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 */ Test("test4",5, 5); /* 1 2 3 4 5 */ Test("test5",1, 5); /* 1 2 3 4 5 6 7 8 9 10 */ Test("test6",2, 5); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ Test("test7",3, 5); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 */ Test("test8",4, 5); /* 1 2 3 4 5 */ Test("test9",5, 1); /* 1 2 3 4 5 6 7 8 9 10 */ Test("test10",5, 2); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ Test("test11",5, 3); /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 */ Test("test12",5, 4); Test("test13",0,0); } }
输出结果:
test1 Begin: 1 columns, 1 rows.
1
test2 Begin: 2 columns, 2 rows.
1 2 4 3
test3 Begin: 4 columns, 4 rows.
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
test4 Begin: 5 columns, 5 rows.
1 2 3 4 5 10 15 20 25 24 23 22 21 16 11 6 7 8 9 14 19 18 17 12 13
test5 Begin: 1 columns, 5 rows.
1 2 3 4 5
test6 Begin: 2 columns, 5 rows.
1 2 4 6 8 10 9 7 5 3
test7 Begin: 3 columns, 5 rows.
1 2 3 6 9 12 15 14 13 10 7 4 5 8 11
test8 Begin: 4 columns, 5 rows.
1 2 3 4 8 12 16 20 19 18 17 13 9 5 6 7 11 15 14 10
test9 Begin: 5 columns, 1 rows.
1 2 3 4 5
test10 Begin: 5 columns, 2 rows.
1 2 3 4 5 10 9 8 7 6
test11 Begin: 5 columns, 3 rows.
1 2 3 4 5 10 15 14 13 12 11 6 7 8 9
test12 Begin: 5 columns, 4 rows.
1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12
test13 Begin: 0 columns, 0 rows.