《剑指offer—面试题29:顺时针打印矩阵》
注明:仅个人学习笔记
参考文献:
【1】https://blog.csdn.net/lilianforever/article/details/51848885
【2】http://wiki.jikexueyuan.com/project/for-offer/question-twenty.html
import java.util.ArrayList;
/**
*
*
* 顺时针打印矩阵
*
*/
public class PrintMatrix29
{
public static ArrayList printMatrix(int[][] matrix)
{
ArrayList<Integer> result = new ArrayList<Integer>();// 用于保存遍历序列
if (matrix == null)
{
return result;
}
int rows = matrix.length;
int cols = 0;
if (rows > 0)
{
cols = matrix[0].length;
} else
{
return result;
}
int start = 0;
while (rows > start * 2 && cols > start * 2)
{
printMatrixInCircle(matrix, rows, cols, start, result);
++start;
}
return result;
}
private static void printMatrixInCircle(int[][] matrix, int rows, int cols, int start, ArrayList<Integer> result)
{
int endX = cols - 1 - start;
int endY = rows - 1 - start;
// 首先,第一行是必存在的,所以直接,从左往右打印第一行
for (int i = start; i <= endX; i++)
{
int number = matrix[start][i];
result.add(number);
System.out.println(number);
}
// 接着,从上往下打印一列,这一列不是一定存在的,存在的前提是,终止行号endY必须大于起始行号,也就是说至少得有两行,才存在这一列的打印
if (endY > start)
{
for (int i = start + 1; i <= endY; i++)
{
int number = matrix[i][endX];
result.add(number);
System.out.println(number);
}
}
// 然后,从右往左打印一行,这一行也不是一定存在的,存在的前提是,至少得有两行两列,也就是说终止行号endY必须大于起始行号,终止列号endY必须大于起始列号endX
if (endY > start && endX > start)
{
for (int i = endX - 1; i >= start; i--)
{
int number = matrix[endY][i];
result.add(number);
System.out.println(number);
}
}
// 最后,从上往下打印一列,这一列同样不是一定存在的,存在的前提是,至少得有三行两列,也就是时候终止行号endY必须大于其实行号2行,并且也得多于两列
if (endX > start && endY > start - 1)
{
for (int i = endY - 1; i >= start + 1; i--)
{
int number = matrix[i][start];
result.add(number);
System.out.println(number);
}
}
}
public static void main(String[] args)
{
int[][] numbers = { { 1, 2, 3, 4, 5 }, { 16, 17, 18, 19, 6 }, { 15, 24, 25, 20, 7 }, { 14, 23, 22, 21, 8 },
{ 13, 12, 11, 10, 9 }, };
printMatrix(numbers);
System.out.println("------------------------------------------");
System.out.println();
int[][] numbers2 = { { 1, 2, 3, 4, 5, 6, 7, 8 }, { 22, 23, 24, 25, 26, 27, 28, 9 },
{ 21, 36, 37, 38, 39, 40, 29, 10 }, { 20, 35, 34, 33, 32, 31, 30, 11 },
{ 19, 18, 17, 16, 15, 14, 13, 12 }, };
printMatrix(numbers2);
System.out.println("------------------------------------------");
System.out.println();
System.out.println();
int[][] numbers3 = { { 1, 2, 3, 4, 5, 6, 7, 8 } };
printMatrix(numbers3);
System.out.println("------------------------------------------");
System.out.println();
System.out.println();
int[][] numbers4 = { { 1, 2, 3, 4, 5, 6, 7, 8 }, { 16, 15, 14, 13, 12, 11, 10, 9 } };
printMatrix(numbers4);
System.out.println("------------------------------------------");
System.out.println();
System.out.println();
System.out.println("------------------------------------------");
System.out.println();
int[][] numbers5 = { { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 } };
printMatrix(numbers5);
System.out.println("------------------------------------------");
System.out.println();
System.out.println();
int[][] numbers6 = { { 0, 1 }, { 15, 2 }, { 14, 3 }, { 13, 4 }, { 12, 5 }, { 11, 6 }, { 10, 7 }, { 9, 8 } };
printMatrix(numbers6);
System.out.println("------------------------------------------");
System.out.println();
System.out.println();
int[][] numbers7 = { { 1, 2 }, { 4, 3 } };
printMatrix(numbers7);
System.out.println("------------------------------------------");
System.out.println();
System.out.println();
int[][] numbers8 = { { 1 } };
printMatrix(numbers8);
System.out.println("-------------------0个元素的数组-----------------------");
System.out.println();
System.out.println();
// 0个元素的数组
printMatrix(new int[][] { {} });
System.out.println("-------------------空数组-----------------------");
// 空数组
printMatrix(null);
}
}