当前位置:网站首页>力扣之螺旋矩阵,一起旋转起来(都能看懂)
力扣之螺旋矩阵,一起旋转起来(都能看懂)
2022-07-21 17:16:00 【学无止境java】
59.螺旋矩阵II
先放题目!
力扣链接点这里
给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
输入:n = 3
输出:[[1,2,3],[8,9,4],[7,6,5]]
思路
模拟顺时针画矩阵的过程:
- 填充上行从左到右
- 填充右列从上到下
- 填充下行从右到左
- 填充左列从下到上
代码加注释
class Solution {
public int[][] generateMatrix(int n) {
//假设n=3
int loop = 0; // 控制循环次数
int[][] res = new int[n][n];
int start = 0; // 每次循环的开始点(start, start)
int count = 1; // 定义填充数字
int i, j;
// 小于n/2为循环次数,例n=3,n^2=9,n/2=1,循环一次即可
while (loop++ < n / 2) {
// 判断边界后,loop从1开始
// 模拟上侧从左到右
for (j = start; j < n - loop; j++) {
//小于n-loop为列坐标,3-1
res[start][j] = count++;
}
// 模拟右侧从上到下
for (i = start; i < n - loop; i++) {
res[i][j] = count++;
}
// 模拟下侧从右到左
for (; j >= loop; j--) {
//j=2(因为上面当j=2时,不符合条件)
res[i][j] = count++;
}
// 模拟左侧从下到上
for (; i >= loop; i--) {
//i=2
res[i][j] = count++;
}
start++;
}
if (n % 2 == 1) {
//n=3,中心还剩一个
res[start][start] = count;
}
return res;
}
}
54. 螺旋矩阵
给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]
提示:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 10 //行数和列数
-100 <= matrix[i][j] <= 100
思路
和上一题大致相同,也是四个循环
先打印最上面一行
再打印右面那一列(注意右上角的元素已经打印过了)
再打印最下面一行(注意右下角的元素打印过了)
再打印最坐面一行(注意左下角的元素打印过了)
代码加注释
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
//定义一个存储结果的变量。
List<Integer> list = new ArrayList<>();
//为空时,直接退出。
if(matrix ==null || matrix.length ==0){
return list;
}
//构造一个 m*n 的一个矩阵
int m = matrix.length; //行(这个不理解的看最下面解释)
int n = matrix[0].length; //列(这个不理解的看最下面解释)
int i =0; //层数(从外向内的层数)
int count = (Math.min(m,n)+1)/2; //统计从外向内的总层数,至少为一层
while(i<count){
//从左往右
//行不变,列逐渐增大,特被这里 n-i是为了控制他在从外到内,第几层。最外面为第0层
// j 为变化的列
for(int j = i;j<n-i;j++){
list.add(matrix[i][j]);
}
//从上往下
//行逐渐增大,列不变
// j 为变化的行
// (n-1)-i 为最右边一列
for(int j = i+1;j<m-i;j++){
list.add(matrix[j][(n-1)-i]);
}
//从右往左
//行不变,列逐渐减小
// j 为变化的列
// (n-1)-(i+1) 这里的 i + 1是为了去除最右下角那个数,
// m-1-i 指向最右边的列, j >= i 是为了保证当行为最后一行
//这里的 (m-1-i) != i 这是用来保证,是属于同一层的
for(int j= (n-1)-(i+1); j>= i && (m-1-i != i); j--){
list.add(matrix[(m-1)-i][j]);
}
//从下往上
//列不变,行逐渐减小
// j 为可变的行
//(m-1)-(i+1) 是为了去除最左上角的数
// j >= i+1,是为了保证当前行为第二行
// (n-1-i) !=i 这是用来保证,是属于同一层的。
for(int j = (m-1)-(i+1);j >= i+1 && (n-1-i) !=i; j--){
list.add(matrix[j][i]);
}
i++; //层数加一,继续向内层递进
}
//返回结果
return list;
}
}
其中
m - 1 - i
是指随着层数增加时,层数的边界所在行(即最上行和最下行的所处的行数),如果出现最上行和最下行是同一行的情况(比如:3行5列的矩阵中,第二层是1行3列的矩阵),此时按顺序打印完第二层第一行后,第一列为空,不打印,折返后如果没有(m
- 1 - i != i)这个限制,会重新打印第二层的第一行,造成结果的值变多。同理可得,n - 1 - i != i。
小知识
二维数组中matrix.length和matrix[0].length
//言简意赅,直接看例子
public class demo1 {
public static void main(String[] args) {
int[][] matrix = new int[3][4];
System.out.println(matrix.length);
System.out.println(matrix[0].length);
}
}
输出:
3 //表示二维数组的行数
4 //表示二维数组的列数
边栏推荐
- Shell练习:统计词频
- 马丁策略的几种应用
- What is RPA? Recommend automated tools that allow e-commerce operators to operate 10 times more efficiently
- 不断提升认知,从而达到交易的最高级别——稳定盈利(二)
- js 对象深拷贝
- Zhang Chi Consulting: How Six Sigma can help companies reduce customer complaints
- 指针深度解刨《四》(指针和数组的 “亲密“ 关系)
- [quick start of flex layout] quickly understand the usage of flex layout, and explain it through four common layout cases [detailed notes, as soon as you see it]
- How to judge the trend in quantitative trading
- Redis的持久化方式RDB和AOF的区别
猜你喜欢
v-7
软考中级【数据库系统工程师】第1章:计算机系统知识,自学软考笔记,备考2022年5月份软考,计算机硬件系统CPU组成指令寄存器组总线输入输出的程序控制方式计算机体系结构与存储系统加密技术流水线技术
指针深度进阶《六》(二维数组相关知识)
Vscode add custom comment
电商大促就靠RPA,摆脱重复劳动,急速提效
RPA是什么?推荐让电商运营10倍提效的自动化工具
Soft test intermediate [Database System Engineer] Chapter 0: how to prepare for the test by self-study, test introduction, test preparation materials, size score distribution in the morning and aftern
Linear table * sequential table (7000 words detailed explanation)
Ffmpeg audio decoding (seconds understand)
Self study golang [Chapter 3: the first go language program] use GoLand to create the first go program, the main function and init function, and use go to run Windows commands and realize the data out
随机推荐
Ffmpeg audio decoding (seconds understand)
Error: L6200E: Symbol keyflag multiply defined (by main.o and key.o).
量化交易日记-回撤分析-2021年02月6日
Seven / four / five layer network model
The difference and implementation of MyISAM and InnoDB in MySQL
Pytoch learning (I) Deep learning review and introduction to pytoch
指针的深度解刨《七》(函数指针相关知识)
指针深度解刨《四》(指针和数组的 “亲密“ 关系)
Soft exam intermediate [Database System Engineer] Chapter 1: computer system knowledge, self-study soft exam notes, preparation for the soft exam in May 2022, computer hardware system CPU composition
指针深度解刨《三》(数组的认知)
【flex布局快速上手】快速理解flex布局用法,通过常见的四个布局案例解释【详细注释,一看就会】
v-7
SPI debugging is not successful, it is likely that you connected the wrong cable!!
Shell exercise: Statistics of word frequency
Self study golang [3.3go language loop statement] for loop syntax structure, omit initial conditions, omit incremental conditions, omit the application of end conditions
Implementation of hardware watchdog
try catch finally 中包含return的几种情况,及返回结果
Continuously improve cognition, so as to reach the highest level of trading - stable profits (I)
Weak foundation, shaking earth and mountains, Niu Ke brushes the title "II"
一个交易系统需要经过几年的考验才算成功的交易系统,盈利需要几年才算稳定?