当前位置:网站首页>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);
}
边栏推荐
- C#winform 窗体事件和委托结合用法
- Demo:pa30 Bank Country Code default CN enhancement
- A quick overview of transformer quantitative papers in emnlp 2020
- Gossip: is rotting meat in the pot to protect students' rights and interests?
- C# 中的转译字符'/b'
- Shell loop exercise
- Shell script learning day01
- Solidity智能合约开发 — 3.3-solidity语法控制结构
- C语言:随机生成数+希尔排序
- MySQL table name area in Linux is not case sensitive
猜你喜欢

如何在 60 秒内去分析和定位问题?

如何获取广告服务流量变现数据,助力广告效果分析?

DASCTF2022.07赋能赛密码wp

Can Linux install sqlserver

DEMO:PA30 银行国家码默认CN 增强

Sword finger offer 58 - I. flip word order

What is the real HTAP? (2) Challenge article

如何在电脑端登陆多个微信

HU相关配置

How to obtain the cash flow data of advertising services to help analyze the advertising effect?
随机推荐
二零二零年终总结
shell脚本学习day01
如何获取广告服务流量变现数据,助力广告效果分析?
The dragon lizard exhibition area plays a new trick this time. Let's see whose DNA moved?
Grandson's questions are difficult, and his son's invigilation is strict. I can't do it. Pay back my school money
CommonTitleBar hide left right
10000 word parsing MySQL index principle -- InnoDB index structure and reading
linux中mysql表名区不区分大小写
2020国际机器翻译大赛:火山翻译力夺五项冠军
Resttemplate connection pool configuration
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly 100 times the improvement of analysis performance
API 版本控制【 Eolink 翻译】
Shell loop exercise
模仿大佬制作的宿舍门禁系统(三)
Redison 3.17.5 release, officially recommended redis client
slf4j如何进行logback配置呢?
C#委托的使用案例
Mqtt instruction send receive request subscription
【Golang】golang开发微信公众号网页授权功能
综合案例、