题目描述
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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 int[] spiralOrder(int[][] matrix) {
if (matrix.length == 0) return new int[0];
int rows = matrix.length;
int columns = matrix[0].length;
int[] res = new int[rows * columns];
int index = 0;
int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
while (left <= right && top <= bottom) {
// 从左向右
for (int i = left; i <= right; i++) res[index++] = matrix[top][i];
// 从上向下
for (int i = top + 1; i <= bottom; i++) res[index++] = matrix[i][right];
// 从右向左,考虑重复:上下两行相等的时候
if (top != bottom)
for (int i = right - 1; i >= left; i--) res[index++] = matrix[bottom][i];
// 从下向上,考虑重复:左右两行相等的时候
if (left != right)
for (int i = bottom - 1; i > top; i--) res[index++] = matrix[i][left];
left++; right--; top++; bottom--;
}
return res;
}
换个写法
public static void spiralOrderPrint(int[][] matrix) {
// 左上角的行和列
int top = 0;
int left = 0;
// 右下角的行和列
int bottom = matrix.length - 1;
int right = matrix[0].length - 1;
while (left <= right && top <= bottom) {
printEdge(matrix, left++, top++, right--, bottom--);
}
}
private static void printEdge(int[][] matrix, int left, int top, int right, int bottom) {
if (left == right) {
for (int i = top; i <= bottom; i++) {
System.out.println(matrix[i][left] + " ");
}
} else if (bottom == top) {
for (int i = left; i <= right; i++) {
System.out.println(matrix[top][i]);
}
} else {
for (int i = left; i <= right; i++) System.out.print(matrix[top][i] + " ");
// 从上向下
for (int i = top + 1; i <= bottom; i++) System.out.print(matrix[i][right] + " ");
// 从右向左
for (int i = right - 1; i >= left; i--) System.out.print(matrix[bottom][i]+" ");
// 从下向上
for (int i = bottom - 1; i > top; i--) System.out.print(matrix[i][left]+ " ");
}
}
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);
}
旋转正方形矩阵
给定一个整型正方形矩阵matrix,请把矩阵调整成顺时针旋转90度的样子
【要求】额外空间复杂度为O(1)
public class RotateMatrix {
public static void rotate(int[][] matrix) {
int left = 0;
int top = 0;
int bottom = matrix.length - 1;
int right = matrix[0].length - 1;
while (left < right) {
rotateEdge(matrix, left++, top++, bottom--, right--);
}
}
private static void rotateEdge(int[][] matrix, int left, int top, int bottom, int right) {
int times = right - left;
int tmp = 0;
for (int i = 0; i < times; i++) {
tmp = matrix[top][left + i];
matrix[top][left + i] = matrix[bottom - i][left];
matrix[bottom - i][left] = matrix[bottom][right - i];
matrix[bottom][right - i] = matrix[top + i][right];
matrix[top + i][right] = tmp;
}
}
private 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);
}
}