问题:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 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.
解答:
public class Test {
public void sortMatrix(int[][] matrix, int startRow, int startCol, int endRow, int
endCol, ArrayList<Integer> result) {
if (startRow < endRow && startCol < endCol) {
// 从左到右
for (int i = startCol; i <= endCol; i++) {
result.add(matrix[startRow][i]);
}
// 从右上到右下
for (int i = startRow + 1; i <= endRow; i++) {
result.add(matrix[i][endCol]);
}
// 从右下到左下
for (int i = endCol - 1; i > startCol; i--) {
result.add(matrix[endRow][i]);
}
// 从左下到左上
for (int i = endRow; i > startRow; i--) {
result.add(matrix[i][startCol]);
}
sortMatrix(matrix, startRow + 1, startCol + 1, endRow - 1, endCol - 1,
result);
} else if (startRow == endRow && startCol < endCol) {
for (int i = startCol; i <= endCol; i++) {
result.add(matrix[startRow][i]);
}
} else if (startRow < endRow && startCol == endCol) {
for (int i = startRow; i <= endRow; i++) {
result.add(matrix[i][startCol]);
}
} else if (startRow == endRow && startCol == endCol) {
result.add(matrix[startRow][startCol]);
}
}
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> result = new ArrayList<>();
if(matrix==null || matrix.length==0) { return result ; }
int startRow = 0; // 开始行
int startCol = 0; // 开始列
int endRow = matrix.length - 1; // 结束行
int endCol = matrix[0].length - 1; // 结束列
sortMatrix(matrix, startRow, startCol,endRow, endCol, result);
return result;
}
public static void main(String[] args) {
int matrix[][] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
Test solution = new Test();
List<Integer> result = solution.printMatrix(matrix);
for (int i = 0; i < result.size(); i++) {
System.out.print(result.get(i));
if (i != result.size() - 1) {
System.out.print(",");
}
}
}
}
延伸:
中午是猿辅导水果时间,小猿会给每个同学发水果。猿辅导有一个矩形的办公区域,共有N 排,每排M个工位。平时小猿按照从第一排到最后一排的顺序发水果,某一天小猿突然发现似乎旋转着发水果是一种更有趣的方式,所以决定试试按照逆时针方向螺旋发水果。
已知每个工位有一个数字,表示该工位员工的工号,每个员工的工号不同。已知小猿从(0, 0)位置开始,按照逆时针螺旋的顺序发水果,请输出收到水果的员工工号序列。
解答:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public void sortMatrix(int[][] matrix, int startRow, int startCol, int endRow, int
endCol, ArrayList<Integer> result) {
if (startRow < endRow && startCol < endCol) {
// 从左上到左下
for (int i = startRow; i <= endRow; i++) {
result.add(matrix[i][startCol]);
}
// 从左下到右下
for (int i = startCol + 1; i <= endCol; i++) {
result.add(matrix[endRow][i]);
}
// 从右下到右上
for (int i = endRow - 1; i >= startRow; i--) {
result.add(matrix[i][endCol]);
}
// 从右上到左上
for (int i = endCol - 1; i > startCol; i--) {
result.add(matrix[startRow][i]);
}
sortMatrix(matrix, startRow + 1, startCol + 1, endRow - 1, endCol - 1,
result);
} else if (startRow == endRow && startCol < endCol) {
for (int i = endCol; i >= startCol; i--) {
result.add(matrix[startRow][i]);
}
} else if (startRow < endRow && startCol == endCol) {
for (int i = endRow; i >= startRow; i--) {
result.add(matrix[i][startCol]);
}
} else if (startRow == endRow && startCol == endCol) {
result.add(matrix[startRow][startCol]);
}
}
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> result = new ArrayList<>();
if(matrix==null || matrix.length==0) { return result ; }
int startRow = 0; // 开始行
int startCol = 0; // 开始列
int endRow = matrix.length - 1; // 结束行
int endCol = matrix[0].length - 1; // 结束列
sortMatrix(matrix, startRow, startCol,endRow, endCol, result);
return result;
}
public static void main(String[] args) {
int n = 0;
int m = 0;
int matrix[][];
Scanner scanner = new Scanner(System.in);
System.out.println("请输入行数及列数:");
String[] firstLine = scanner.nextLine().split(" ");
n = Integer.parseInt(firstLine[0]);
m = Integer.parseInt(firstLine[1]);
matrix = new int[n][m];
Test test = new Test();
for (int i = 0; i < n; i++) {
String[] str = scanner.nextLine().split(" ");
for (int j = 0; j < m; j++) {
matrix[i][j] = Integer.parseInt(str[j]);
}
}
// long startTime = System.currentTimeMillis(); //获取开始时间
List<Integer> result = test.printMatrix(matrix);
// long endTime = System.currentTimeMillis(); //获取结束时间
// System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间
for (int i = 0; i < result.size(); i++) {
System.out.print(result.get(i));
if (i != result.size() - 1) {
System.out.print(",");
}
}
}
}