1. 旋转图像(中等)
地址: https://leetcode-cn.com/problems/rotate-image/
2022/02/20-2022/7/18
做题反思:
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 tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
for (int[] row : matrix) {
reverse(row);
}
}
void reverse(int[] arr) {
int i = 0, j = arr.length - 1, temp;
while (j > i) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
}
2. 螺旋矩阵(中等)
地址: https://leetcode-cn.com/problems/spiral-matrix/
2022/02/20-2022/7/18
做题反思:
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 i = left_bound; i <= right_bound; i++) {
res.add(matrix[upper_bound][i]);
}
upper_bound++;
}
if (right_bound >= left_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 i = right_bound; i >= left_bound; i--) {
res.add(matrix[lower_bound][i]);
}
lower_bound--;
}
if (right_bound >= left_bound) {
for (int i = lower_bound; i >= upper_bound; i--) {
res.add(matrix[i][left_bound]);
}
left_bound++;
}
}
return res;
}
}
3. 螺旋矩阵 II(中等)
地址: https://leetcode-cn.com/problems/spiral-matrix-ii/
2022/02/20
做题反思: