当前位置:网站首页>剑指 Offer 29. 顺时针打印矩阵
剑指 Offer 29. 顺时针打印矩阵
2022-07-02 01:54:00 【Yake1965】
剑指 Offer 29. 顺时针打印矩阵
class Solution {
public int[] spiralOrder(int[][] matrix) {
int m = matrix.length;
if (m == 0) return new int[]{
};
int n = matrix[0].length;
int[] res = new int[m * n];
int up = 0, down = m - 1;
int left = 0, right = n - 1;
int idx = 0;
while (left <= right && up <= down) {
for (int i = left; i <= right; i++) {
res[idx++] = matrix[up][i];
}
up++;
for (int i = up; i <= down; i++) {
res[idx++] = matrix[i][right];
}
right--;
if (left > right || up > down) break;
for (int i = right; i >= left; i--) {
res[idx++] = matrix[down][i];
}
down--;
for (int i = down; i >= up; i--) {
res[idx++] = matrix[i][left];
}
left++;
}
return res;
}
}
边栏推荐
- Volume compression, decompression
- Laravel artisan 常用命令
- Redis有序集合如何使用
- Software No.1
- The smart Park "ZhongGuanCun No.1" subverts your understanding of the park
- uTools
- 分卷压缩,解压
- Basic concepts of machine learning
- matlab 使用 resample 完成重采样
- SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
猜你喜欢
Penser au jeu 15: penser au service complet et au sous - service
MPLS experiment operation
321. Chessboard segmentation (2D interval DP)
golang---锁
The technology boss is ready, and the topic of position C is up to you
The concept, function, characteristics, creation and deletion of MySQL constraints
企业应该选择无服务器计算吗?
Three core problems of concurrent programming
matlab 使用 resample 完成重采样
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
随机推荐
Laravel artisan 常用命令
JMeter (I) - download, installation and plug-in management
TSINGSEE青犀平台如何实现同一节点同时播放多个视频?
Implementation of Weibo system based on SSM
人工智能在网络安全中的作用
Penser au jeu 15: penser au service complet et au sous - service
Volume compression, decompression
np. Where and torch Where usage
Three core problems of concurrent programming
Software No.1
matlab 使用 resample 完成重采样
Construction and maintenance of business websites [13]
Based on configured schedule, the given trigger will never fire
Data analysis on the disaster of Titanic
[Floyd] post disaster reconstruction
如何远程、在线调试app?
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
matlab 使用 audioread 、 sound 读取和播放 wav 文件
[rust web rokcet Series 2] connect the database and add, delete, modify and check curd
* and & symbols in C language