题目一:螺旋矩阵 II
leetcode.cn/problems/sp…
给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
示例 1:
输入: n = 3
输出: [[1,2,3],[8,9,4],[7,6,5]]
示例 2:
输入: n = 1
输出: [[1]]
提示:
1 <= n <= 20
解法1:
static class Solution2 {
public int[][] generateMatrix(int n) {
int[][] result = new int[n][n];
int startx = 0, starty = 0;
int count = 1;
int i = 0, j = 0;
int offset = 1;
int k = n / 2;
while (k > 0){
//左闭右开区间
//向右移动
for(j = starty; j < n - offset; j++){
result[startx][j] = count++;
}
//向下移动
for(i = startx; i < n - offset; i++){
result[i][j] = count++;
}
//向左移动
for(; j > starty; j--){
result[i][j] = count++;
}
//向上移动
for(; i >startx; i--){
result[i][j] = count++;
}
//进入下一圈
startx++;
starty++;
offset++;
k--;
}
if(n%2 ==1){
result[startx][starty] = count;
}
return result;
}
}
解法二:
static class Solution {
public int[][] generateMatrix(int n) {
int[][] result = new int[n][n];
int left = 0, right = n - 1;
int top = 0, bottom = n - 1;
int count = 1;
int ans = n * n;
while(left < right || top < bottom){ //这里改成count<=ans也可以
//向右移动
for(int i = left; i <= right; i++){//这里是左闭右闭区间
result[top][i] = count++;
}
top++; //第一行遍历完,上边界-1
//向下移动
for(int i = top; i <= bottom; i++){
result[i][right] = count++;
}
right--;//最后一列遍历完,右边界-1
//向左移动
for(int i = right; i >= left; i--){
result[bottom][i] = count++;
}
bottom--;//最后一行遍历完,下边界-1
for(int i = bottom; i >= top; i--){
result[i][left] = count++;
}
left++;//第一列遍历完,左边界+1
}
return result;
}
}
这里解法二明显更好一点,用到了更少的变量,并且不用特殊处理奇数的情况。
这里第二道题目可以用同样的思路解决问题,只不过因为矩阵不是正方形,所以在每次找完一行或者一列判断一下是否超出返回即可。
题目二
螺旋矩阵
leetcode.cn/problems/sp…
给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。
示例 1:
输入: matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出: [1,2,3,6,9,8,7,4,5]
示例 2:
输入: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]
提示:
m == matrix.lengthn == matrix[i].length1 <= m, n <= 10-100 <= matrix[i][j] <= 100
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>();
int left = 0, right = matrix[0].length-1;
int top = 0, bottom = matrix.length-1;
while (left <= right && top <= bottom){
for(int i = left; i <= right; i++){
list.add(matrix[top][i]);
}
top++;
if(top> bottom) break;
for(int i = top; i <= bottom; i++){
list.add(matrix[i][right]);
}
right--;
if(left > right) break;
for(int i = right; i >= left; i--){
list.add(matrix[bottom][i]);
}
bottom--;
if (top > bottom) break;
for(int i = bottom; i >= top; i--){
list.add(matrix[i][left]);
}
left++;
if(left > right) break;
}
return list;
}
}
题目三
class Solution {
public int[] spiralArray(int[][] array) {
if(array.length == 0 || array == null || array[0].length == 0){
return new int[0];
}
int left = 0, right = array[0].length-1;
int top = 0, bottom = array.length-1;
int[] result = new int[(right+1) * (bottom+1)];
int index = 0;
while (left <= right && top <= bottom){
for(int i = left; i <= right; i++){
result[index++] = array[top][i];
}
top++;
if(top> bottom) break;
for(int i = top; i <= bottom; i++){
result[index++] = array[i][right];
}
right--;
if(left > right) break;
for(int i = right; i >= left; i--){
result[index++] = array[bottom][i];
}
bottom--;
if (top > bottom) break;
for(int i = bottom; i >= top; i--){
result[index++] = array[i][left];
}
left++;
if(left > right) break;
}
return result;
}
}
这里多了一个要判断输入是否为空的问题,在这里就有一个问题: \