给你一个正整数 n
,生成一个包含 1
到 n^2
所有元素,且元素按顺时针顺序螺旋排列的 n x n
正方形矩阵 matrix
。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2bgTWd6K-1680958210578)(螺旋矩阵及其相关题目.assets/image-20230407232740223.png)]
输入:n = 3
输出:[[1,2,3],[8,9,4],[7,6,5]]
示例 2:
输入:n = 1
输出:[[1]]
提示:
1 <= n <= 20
比较基础,主要是容易绕晕
尤其是for循环的结束条件,在大脑或者草稿纸上遍历一遍。
JAVA
class Solution {
public int[][] generateMatrix(int n) {
int loop = 0;
int start = 0;
int[][] res = new int[n][n];
int offset = 1;
int i,j;
int count = 1;
while (loop < n / 2){
for (j = start;j < n - offset;j++){
res[start][j] = count++;
}
for (i = start;i < n - offset;i++){
res[i][j] = count++;
}
for (;j > loop;j--){
res[i][j] = count++;
}
for (;i > loop;i--){
res[i][j] = count++;
}
loop++;
offset++;
start++;
}
if (n % 2 == 1) {
res[start][start] = count;
}
return res;
}
}
JavaScript
/**
* @param {number} n
* @return {number[][]}
*/
var generateMatrix = function(n) {
let startIndex = 0,loop = 0;
let count = 1,offset = 1;
let od = Math.floor(n / 2);
let i,j;
let res = new Array(n).fill(0).map(() => new Array(n).fill(0));
while (loop < od){
for (j = startIndex;j < n - offset;j++){
res[startIndex][j] = count++;
}
for (i = startIndex;i < n - offset;i++){
res[i][j] = count++;
}
// 每个for循环中的结束条件都要注意
for (;j > startIndex;j--){
res[i][j] = count++;
}
for (;i > startIndex;i--){
res[i][j] = count++
}
loop++;
offset++;
startIndex++;
}
if (n % 2 == 1){
res[startIndex][startIndex] = count;
}
return res;
};
给你一个 m
行 n
列的矩阵 matrix
,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Wm6XMQjj-1680958210581)(4.螺旋矩阵及其相关题目.assets/image-20230408200043752.png)]
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qUMCG4kt-1680958210582)(4.螺旋矩阵及其相关题目.assets/image-20230408200310199.png)]
输入: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.length n == matrix[i].length 1 <= m, n <= 10
-100 <= matrix[i][j] <= 100
基于正方形矩阵的方法,列举了很多长方形矩阵例子,归纳总结出的后半部分
m是行数,n是列数; i 与 m相关,移动i 就是移动行,纵向移动;j 与n相关,移动j就是移动列 横向移动
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
let m = matrix.length;
let n = matrix[0].length;
let res = new Array(m * n);
let start = 0;
let offset = 1;
let i,j;
let count = 0;
let loop = Math.floor(Math.min(m,n) / 2);
while (loop--){
for (j = start;j < n - offset;j++){
res[count++] = matrix[start][j];
}
for (i = start;i < m - offset;i++){
res[count++] = matrix[i][j];
}
for (; j > start;j--){
res[count++] = matrix[i][j];
}
for (; i > start;i--){
res[count++] = matrix[i][j];
}
offset++;
start++;
}
if (Math.min(m,n) % 2 == 1){
if (m < n){
for (let k = start;k < start + n - m + 1;k++){
res[count++] = matrix[start][k];
}
} else {
for (let k = start;k < start + m - n + 1;k++){
res[count++] = matrix[k][start];
}
}
}
return res;
};
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
示例 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]
限制:
0 <= matrix.length <= 100
0 <= matrix[i].length <= 100
与上一题基本一致,但条件有一些不同,矩阵可以是空的,所以多了个if条件,注意审题
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
if (!matrix.length || !matrix[0].length) {
return [];
}
let m = matrix.length;
let n = matrix[0].length;
let res = new Array(m * n);
let start = 0;
let offset = 1;
let i,j;
let count = 0;
let loop = Math.floor(Math.min(m,n) / 2);
while (loop--){
for (j = start;j < n - offset;j++){
res[count++] = matrix[start][j];
}
for (i = start;i < m - offset;i++){
res[count++] = matrix[i][j];
}
for (; j > start;j--){
res[count++] = matrix[i][j];
}
for (; i > start;i--){
res[count++] = matrix[i][j];
}
offset++;
start++;
}
if (Math.min(m,n) % 2 == 1){
if (m < n){
for (let k = start;k < start + n - m + 1;k++){
res[count++] = matrix[start][k];
}
} else {
for (let k = start;k < start + m - n + 1;k++){
res[count++] = matrix[k][start];
}
}
}
return res;
};
思路不难,主要是在大脑中模拟过程(大脑不行,在草稿纸上模拟)
主要考察对于遍历过程的掌握,含糊过去就很容易出错。