当前位置:网站首页>剑指 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;
}
}
边栏推荐
- uTools
- Discussion on the idea of platform construction
- From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
- Android high frequency network interview topic must know and be able to compare Android development environment
- Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
- new和malloc的区别
- Data analysis on the disaster of Titanic
- 企业应该选择无服务器计算吗?
- matlab 实现语音信号重采样和归一化,并播放比对效果
- 卷積神經網絡(包含代碼與相應圖解)
猜你喜欢

Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories

Android high frequency network interview topic must know and be able to compare Android development environment

MySQL约束与多表查询实例分析

1222. Password dropping (interval DP, bracket matching)

并发编程的三大核心问题

SQLite 3 of embedded database

PR second training

Matlab uses audioread and sound to read and play WAV files

城市选择器组件实现原理

734. Energy stone (greed, backpack)
随机推荐
MySQL主从延迟问题怎么解决
Redis有序集合如何使用
ES6 new method of string
Exception handling of class C in yyds dry goods inventory
Introduction to ffmpeg Lib
This is the form of the K-line diagram (pithy formula)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
现货黄金分析的技巧有什么呢?
自动浏览拼多多商品
mysql列转行函数指的是什么
Based on configured schedule, the given trigger will never fire
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
Construction and maintenance of business websites [10]
The role of artificial intelligence in network security
There are spaces in the for loop variable in the shell -- IFS variable
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
Discussion on the idea of platform construction
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
如何用一款产品推动「品牌的惊险一跃」?