当前位置:网站首页>Leetcode59. 螺旋矩阵 II
Leetcode59. 螺旋矩阵 II
2022-07-28 22:06:00 【Java全栈研发大联盟】
题目传送地址: https://leetcode.cn/problems/spiral-matrix-ii/
运行效率:
代码如下:
class Solution {
public static int[][] generateMatrix(int n) {
int[][] matrix = new int[n][n];
int count = 1;
String coordinate = "0:0:0";
while (count <= Math.pow(n, 2)) {
String[] split = coordinate.split(":");
Integer row = Integer.parseInt(split[0]);
Integer col = Integer.parseInt(split[1]);
matrix[row][col] = count;
coordinate = getNextCoordinate(coordinate, matrix);
count++;
}
return matrix;
}
/** * 获取下一个坐标的位置 * * @param coordinate 当前坐标所在的行 当前坐标所在的列 当前坐标前进的方向 * @param matrix * @return */
public static String getNextCoordinate(String coordinate, int[][] matrix) {
String[] split = coordinate.split(":");
Integer row = Integer.parseInt(split[0]);
Integer col = Integer.parseInt(split[1]);
Integer direction = Integer.parseInt(split[2]);
//从左往右
if (direction == 0) {
//如果右边还有可移动的地方
if (col + 1 <= matrix[0].length - 1 && matrix[row][col + 1] == 0) {
col++;
} else {
direction = 1; // 把坐标前进的方向改为从上到下
row++; //列坐标不变,行坐标下移
}
return row + ":" + col + ":" + direction;
}
//从上往下
if (direction == 1) {
//如果下边还有可移动的地方
if (row + 1 <= matrix.length - 1 && matrix[row + 1][col] == 0) {
row++;
} else {
direction = 2; // 把坐标前进的方向改为从右往左
col--; //行坐标不变,列坐标左移
}
return row + ":" + col + ":" + direction;
}
//从右往左
if (direction == 2) {
//如果左边还有可移动的地方
if (col - 1 >= 0 && matrix[row][col - 1] == 0) {
col--;
} else {
direction = 3; // 把坐标前进的方向改为从下往上
row--; //列坐标不变,行坐标上移
}
return row + ":" + col + ":" + direction;
}
//从下往上
if (direction == 3) {
//如果上边还有可移动的地方
if (row - 1 >= 0 && matrix[row - 1][col] == 0) {
row--;
} else {
direction = 0; // 把坐标前进的方向改为从左往右
col++; //行坐标不变,列坐标右移
}
return row + ":" + col + ":" + direction;
}
return row + ":" + col + ":" + direction;
}
}
边栏推荐
- 剑指 Offer 64. 求1+2+…+n,逻辑运算符短路效应
- Equipped with a new generation of ultra safe cellular batteries, Sihao aipao is available from 139900 yuan
- [self] - brush questions set
- Tyrosine decarboxylase -- characteristics of tyrosine decarboxylase of Streptococcus faecalis in Worthington
- Learn browser decoding from XSS payload
- 迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables
- 有效供应链管理的八大绩效分析指标(上)
- Worthington丨Worthington胰蛋白酶抑制剂说明书
- Codeforces Round #474 (Div. 1 + Div. 2) - C, F
- 搭载新一代超安全蜂窝电池,思皓爱跑上市13.99万元起售
猜你喜欢
随机推荐
How to add the index of a set in mongodb to another set in mongodb
Worthington核糖核酸酶B历史和化学性质说明
剑指 Offer 64. 求1+2+…+n,逻辑运算符短路效应
机器学习问题笔记
GhostNets on Heterogeneous Devices via Cheap Operations
Use pytoch to quickly train the network model
RHCE the next day
Zero view h5s video platform getUserInfo information disclosure vulnerability cnvd-2020-67113
商家对积分体系运营的两个误解
AUTOCAD——Excel表格导入CAD、CAD合并两兄弟
PowerCLi 批量添加esxi到vCenter
SAP oracle 复制新实例后数据库远程连接报错 ora-01031
A new generation of ultra safe cellular battery, Sihao aipao, will be available from 139900 yuan
Zabbix 5.0 使用自带Redis模版监控
[self] - question brushing - string
Worthington核糖核酸测定详细攻略
pip镜像下载
2022 R2 mobile pressure vessel filling test question simulation test platform operation
Ape anthropology topic 20
使用Pytorch快速训练网络模型





![[self] - question brushing - string](/img/80/7d571e4f82caaad00d0a20152832c2.png)



