(中等)59. 螺旋矩阵II
因为我是先做的JZ29,所以看到这题的时候,几乎就是一样的题,代码都几乎一样,思路解析在下一节JZ29中
class Solution {
public int[][] generateMatrix(int n) {
int left = 0;
int right = n - 1;
int up = 0;
int down = n - 1;
int[][] res = new int[n][n];
int index = 1;
while (left <= right && up <= down) {
for (int i = left; i <= right; i++) {
res[up][i] = index++;
}
up++;
if (up > down) {
break;
}
for (int i = up; i <= down; i++) {
res[i][right] = index++;
}
right--;
if (left > right) {
break;
}
for (int i = right; i >= left; i--) {
res[down][i] = index++;
}
down--;
if (up > down) {
break;
}
for (int i = down; i >= up; i--) {
res[i][left] = index++;
}
left++;
if (left > right) {
break;
}
}
return res;
}
}
(中等)54. 螺旋矩阵
思路和JZ29是一样的,代码几乎也是一样的
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int up = 0;
int left = 0;
int down = matrix.length - 1;
int right = matrix[0].length - 1;
ArrayList<Integer> list = new ArrayList<>();
while (left <= right && up <= down) {
for (int i = left; i <= right; i++) {
list.add(matrix[up][i]);
}
up++;
if (up > down) {
break;
}
for (int i = up; i <= down; i++) {
list.add(matrix[i][right]);
}
right--;
if (left > right) {
break;
}
for (int i = right; i >= left; i--) {
list.add(matrix[down][i]);
}
down--;
if (up > down) {
break;
}
for (int i = down; i >= up; i--) {
list.add(matrix[i][left]);
}
left++;
if (left > right) {
break;
}
}
return list;
}
}
(简单)JZ29 顺时针打印矩阵
我的代码
这是一道模拟题,就按照正常思路模拟即可
我用了visit标记数组,标记是否访问过这个位置
使用变量cnt记录当前访问过几个
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int[][] matrix) {
int x = 0;
int y = 0;
int width = matrix[0].length;
int height = matrix.length;
int cnt = 0;
boolean[][] visit = new boolean[height][width];
ArrayList<Integer> list = new ArrayList<>();
while (cnt < width * height) {
while (cnt < width * height && y < width && !visit[x][y]) {
list.add(matrix[x][y]);
visit[x][y] = true;
cnt++;
y++;
}
y--;
x++;
while (cnt < width * height && x < height && !visit[x][y]) {
list.add(matrix[x][y]);
visit[x][y] = true;
cnt++;
x++;
}
x--;
y--;
while (cnt < width * height && y >= 0 && !visit[x][y]) {
list.add(matrix[x][y]);
visit[x][y] = true;
cnt++;
y--;
}
y++;
x--;
while (cnt < width * height && x >= 0 && !visit[x][y]) {
list.add(matrix[x][y]);
visit[x][y] = true;
cnt++;
x--;
}
x++;
y++;
}
return list;
}
}
牛客官方的代码写的比我这个要更简洁一些
具体做法:
- 首先排除特殊情况,即矩阵为空的情况
- 设置矩阵的四个边界值,开始准备螺旋遍历矩阵,遍历的截止点是左右边界或者上下边界重合
- 首先对最上面一排从左到右进行遍历输出,到达最右边后第一排就输出完了,上边界相应就往下一行,要判断上下边界是否相遇相交
- 然后输出到了右边,正好就对最右边一列从上到下输出,到底后最右边一列已经输出完了,右边界就相应往左一列,要判断左右边界是否相遇相交
- 然后对最下面一排从右到左进行遍历输出,到达最左边后最下面一排就输出完了,下边界相应就往上一行,要判断上下边界是否相遇相交
- 然后输出到了左边,正好就对左边一列从下到上输出,到顶后最左边一列已经输出完了,左边界就相应往右一列,要判断左右边界是否相遇相交
- 重复3-6步骤,直到循环结束
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int[][] matrix) {
ArrayList<Integer> list = new ArrayList<>();
if (matrix.length == 0) {
return list;
}
//左边界
int left = 0;
//右边界
int right = matrix[0].length - 1;
//上边界
int up = 0;
//下边界
int down = matrix.length - 1;
while (left <= right && up <= down) {
//上边界,从左到右
for (int i = left; i <= right; i++) {
list.add(matrix[up][i]);
}
up++;
if (up > down) {
break;
}
//右边界,从上到下
for (int i = up; i <= down; i++) {
list.add(matrix[i][right]);
}
right--;
if (left > right) {
break;
}
//下边界,从右到左
for (int i = right; i >= left; i--) {
list.add(matrix[down][i]);
}
down--;
if (up > down) {
break;
}
//左边界,从下到上
for (int i = down; i >= up; i--) {
list.add(matrix[i][left]);
}
left++;
if (left > right) {
break;
}
}
return list;
}
}