当前位置:网站首页>Leetcode54. Spiral matrix
Leetcode54. Spiral matrix
2022-07-27 08:00:00 【Java full stack R & D Alliance】
Title transmission address : https://leetcode.cn/problems/spiral-matrix/
Operational efficiency :
The code is as follows :
public static List<Integer> spiralOrder(int[][] matrix) {
HashSet<String> set = new HashSet<>();
int count = matrix.length * matrix[0].length;
int direction = 0;// The direction we are going 0: From left to right 1: From the top down 2: From right to left 3: From bottom to top
List<Integer> list = new ArrayList<>();
int rowIndex = 0;
int colIndex = 0;
while (list.size() < count) {
int num = matrix[rowIndex][colIndex];
list.add(num);
String coordinate = rowIndex + ":" + colIndex;// coordinate
set.add(coordinate);
// Verify whether the next coordinate in the same direction of the current coordinate is out of bounds
boolean validNextCoordinate = validNextCoordinate(direction, coordinate, set, matrix);
// From left to right
if (direction == 0) {
if (validNextCoordinate) {
// If you don't cross the border , Then continue to move to the right
colIndex++;
} else {
// If you cross the border , Change direction
direction = 1;
rowIndex++; // Change direction , Then the coordinates of the next position should be below the current coordinates
}
continue;
}
// From the top down
if (direction == 1) {
if (validNextCoordinate) {
// If you don't cross the border , Then continue to move down
rowIndex++;
} else {
// If you cross the border , Change direction Then the next step is to go left
direction = 2;
colIndex--;
}
continue;
}
// From right to left
if (direction == 2) {
if (validNextCoordinate) {
// If you don't cross the border , Then keep moving to the left
colIndex--;
} else {
// If you cross the border , Change direction Then the next step is to go up
direction = 3;
rowIndex--;
}
continue;
}
// From bottom to top
if (direction == 3) {
if (validNextCoordinate) {
// If you don't cross the border , Then continue to move up
rowIndex--;
} else {
// If you cross the border , Change direction Then the next step is to go to the right
direction = 0;
colIndex++;
}
}
}
return list;
}
/** * Verify whether the next coordinate in the same direction of the current coordinate is out of bounds * * @param direction * @param coordinate * @param set * @param matrix * @return */
public static boolean validNextCoordinate(Integer direction, String coordinate, HashSet<String> set, int[][] matrix) {
String[] split = coordinate.split(":"); // Current coordinate position
int row = Integer.parseInt(split[0]); // Coordinate position of current line
int col = Integer.parseInt(split[1]); // Current column coordinate position
if (direction == 0) {
// If it is from left to right
col++; // Row coordinates unchanged , Shift the column coordinates to the right
}
if (direction == 1) {
// If it's from top to bottom
row++; // The column coordinates do not change , Move the row coordinates down
}
if (direction == 2) {
// If it's from right to left
col--; // Row coordinates unchanged , The column coordinates move left
}
if (direction == 3) {
// If it's from bottom to top
row--; // The column coordinates do not change , Move the row coordinates up
}
// First of all, make sure that the coordinates of the next position cannot exceed matrix The scope of
if (row >= matrix.length || row < 0 || col >= matrix[0].length || col < 0) {
return false;
}
// Secondly, the coordinates of the next position cannot be set Inside
String nextCoordinate = row + ":" + col;
return !set.contains(nextCoordinate);
}
边栏推荐
- Redisson 3.17.5 发布,官方推荐的 Redis 客户端
- Grandson's questions are difficult, and his son's invigilation is strict. I can't do it. Pay back my school money
- Enhancement: BTE process introduction
- The token verification of applet message push configuration failed. Please check and confirm
- Promise详解
- CommonTitleBar hide left right
- Stored procedures and functions
- Sword finger offer 58 - I. flip word order
- Install tensorflow
- Kalibr calibration realsensed435i -- multi camera calibration
猜你喜欢

Demo submit a program and obtain ALV data of the program

C# 事件用法案例 订阅事件+=
![[resolved] the new version of pychart (2022) connects to the server to upload files and reports an error of](/img/13/573d8ae55eb25580a5628b469dfab3.png)
[resolved] the new version of pychart (2022) connects to the server to upload files and reports an error of "command Rsync is not found in path", and the files cannot be synchronized

杂谈:把肉都烂在锅里就是保障学生权益了?

How to update PIP3? And running PIP as the 'root' user can result in broken permissions and conflicting behavior
![[applet] how to get wechat applet code upload key?](/img/b4/76e2f12269601c0a969a709ff8a397.png)
[applet] how to get wechat applet code upload key?

C commissioned use cases

An ordinary autumn recruitment experience

Enhancement: BTE process introduction

LeetCode56. 合并区间
随机推荐
DEMO SUBMIT 某程序并获取该程序ALV数据
Internet of things industrial UART serial port to WiFi to wired network port to Ethernet Gateway WiFi module selection
[resolved] the new version of pychart (2022) connects to the server to upload files and reports an error of "command Rsync is not found in path", and the files cannot be synchronized
npm的使用
代码接口自动化的有点
存储过程与函数
Can Linux install sqlserver
JS access cookie example
Day111. Shangyitong: integrate nuxt framework, front page data, hospital details page
大家节日快乐哈
Harbor can't log in with the correct password
C# 中的转译字符'/b'
The dragon lizard exhibition area plays a new trick this time. Let's see whose DNA moved?
鲁迅:我不记得说没说过,要不你自己查!
API version control [eolink translation]
Bash: sudo: command not found in container
Translation character '/b' in C #
【已解决】单点登录成功SSO转发,转发URL中带参数导致报错There was an unexpected error (type=Internal Server Error, status=500)
Shell enterprise interview exercise
C语言:随机生成数+插入排序