当前位置:网站首页>力扣之螺旋矩阵,一起旋转起来(都能看懂)
力扣之螺旋矩阵,一起旋转起来(都能看懂)
2022-06-30 12:15: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 //表示二维数组的列数
边栏推荐
- QT implementation dynamic navigation bar
- How to detect 3D line spectral confocal sensors in semiconductors
- FlinkSQL自定义UDAF使用的三种方式
- Tencent two sides: @bean and @component are used on the same class. What happens?
- Clipboardjs - development learning summary 1
- Redis configuration files and new data types
- How to use AI technology to optimize the independent station customer service system? Listen to the experts!
- 药店管理系统
- Three ways for flinksql to customize udaf
- 海思3559萬能平臺搭建:獲取數據幀修改後編碼
猜你喜欢
Reading the table data of Tencent documents in the applet
rpm2rpm 打包步骤
Redis installation on Linux system
Browser plays RTSP video based on nodejs
机器学习笔记 - 自相关和偏自相关简介
【一天学awk】内置变量的使用
Hisilicon 3559 universal platform construction: introduction to YUV format
江西财经大学智慧江财登录分析
Building of Hisilicon 3559 universal platform: obtaining the modified code of data frame
How to detect 3D line spectral confocal sensors in semiconductors
随机推荐
如何利用AI技术优化独立站客服系统?听听专家怎么说!
Inner join and outer join of MySQL tables
Splitting e-commerce systems into micro services
Android development interview real question advanced version (with answer analysis)
Redis configuration files and new data types
两批次纯牛奶不合格?麦趣尔回应:正对产品大批量排查抽检
Introduction to the novelty of substrate source code: comprehensive update of Boca system Boca weight calculation, optimization and adjustment of governance version 2.0
数据仓库建设之确定主题域
What are the applications of 3D visual inspection in production flow
Layout of pyqt5 interface and loading of resource files
[target tracking] |pytracking configuring win to compile prroi_ pool. pyd
Shell编程概述
NoSQL - redis configuration and optimization
How to detect 3D line spectral confocal sensors in semiconductors
How to select an OLAP database engine?
dataworks 同步maxcomputer 到sqlserver ,汉字变乱码了,请问怎么解决
[300+ continuous sharing of selected interview questions from large manufacturers] column on interview questions of big data operation and maintenance (II)
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
Redis的基本操作的命令
Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines