顺/逆时针旋转矩阵
LeetCode 48.旋转图像
常规的思路就是去寻找原始坐标和旋转后坐标的映射规律,但我们是否可以让思维跳跃跳跃,尝试把矩阵进行反转、镜像对称等操作,可能会出现新的突破口。
我们可以先将 n x n 矩阵 matrix 按照左上到右下的对角线进行镜像对称,然后再对矩阵的每一行进行反转,结果就是 matrix 顺时针旋转 90 度的结果:。
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 26 27 28 29 30 31 32
| class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } for (int[] row : matrix) { reverse(row); } }
void reverse(int[] arr) { int i = 0, j = arr.length - 1; while (j > i) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } }
|
扩展:如何将矩阵逆时针旋转90度
思路是类似的,只要通过另一条对角线镜像对称矩阵,然后再反转每一行,就得到了逆时针旋转矩阵的结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution {
public void rotate2(int[][] matrix) { int n = matrix.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n - i; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[n - j - 1][n - i - 1]; matrix[n - j - 1][n - i - 1] = temp; } } for (int[] row : matrix) { reverse(row); } }
void reverse(int[] arr) { } }
|
解法2:先翻转再对称
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 26
| public void rotate(int[][] matrix) { int n = matrix.length; for (int[] arr : matrix) { reverse(arr); } for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int tmp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = tmp; } } } void reverse(int[] nums) { int n = nums.length; int left = 0; int right = n - 1; while (left < right) { int tmp = nums[left]; nums[left] = nums[right]; nums[right] = tmp; left++; right--; } }
|
矩阵的螺旋遍历
LeetCode 54.螺旋矩阵
解题的核心思路是按照右、下、左、上的顺序遍历数组,并使用四个变量圈定未遍历元素的边界,随着螺旋遍历,相应的边界会收缩,直到螺旋遍历完整个数组。
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| class Solution { public List<Integer> spiralOrder(int[][] matrix) { int m = matrix.length, n = matrix[0].length; int upper_bound = 0, lower_bound = m - 1; int left_bound = 0, right_bound = n - 1; List<Integer> res = new LinkedList<>(); while (res.size() < m * n) { if (upper_bound <= lower_bound) { for (int j = left_bound; j <= right_bound; j++) { res.add(matrix[upper_bound][j]); } upper_bound++; } if (left_bound <= right_bound) { for (int i = upper_bound; i <= lower_bound; i++) { res.add(matrix[i][right_bound]); } right_bound--; } if (upper_bound <= lower_bound) { for (int j = right_bound; j >= left_bound; j--) { res.add(matrix[lower_bound][j]); } lower_bound--; } if (left_bound <= right_bound) { for (int i = lower_bound; i >= upper_bound; i--) { res.add(matrix[i][left_bound]); } left_bound++; } } return res; } }
|
LeetCode 59.螺旋矩阵Ⅱ
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| class Solution { public int[][] generateMatrix(int n) { int[][] matrix = new int[n][n]; int upper_bound = 0, lower_bound = n - 1; int left_bound = 0, right_bound = n - 1; int num = 1; while (num <= n * n) { if (upper_bound <= lower_bound) { for (int j = left_bound; j <= right_bound; j++) { matrix[upper_bound][j] = num++; } upper_bound++; } if (left_bound <= right_bound) { for (int i = upper_bound; i <= lower_bound; i++) { matrix[i][right_bound] = num++; } right_bound--; } if (upper_bound <= lower_bound) { for (int j = right_bound; j >= left_bound; j--) { matrix[lower_bound][j] = num++; } lower_bound--; } if (left_bound <= right_bound) { for (int i = lower_bound; i >= upper_bound; i--) { matrix[i][left_bound] = num++; } left_bound++; } } return matrix; } }
|