当前位置:网站首页>剑指 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;
}
}
边栏推荐
- leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
- [Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
- Android: the kotlin language uses grendao3, a cross platform app development framework
- uTools
- 三分钟学会基础k线图知识
- MySQL中一条SQL是怎么执行的
- 2022 Q2 - 提升技能的技巧总结
- Based on configured schedule, the given trigger will never fire
- The difference between new and malloc
- Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
猜你喜欢

Basic concepts of machine learning

Data analysis on the disaster of Titanic

KS006基于SSM实现学生成绩管理系统

卷積神經網絡(包含代碼與相應圖解)

Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)

Discussion on the idea of platform construction

1069. Division of convex polygons (thinking, interval DP)

Convolutional neural network (including code and corresponding diagram)

Using tabbar in wechat applet

321. Chessboard segmentation (2D interval DP)
随机推荐
分卷压缩,解压
如何远程、在线调试app?
Construction and maintenance of business websites [14]
遷移雲計算工作負載的四個基本策略
MySQL主从延迟问题怎么解决
Deep learning: a solution to over fitting in deep neural networks
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
Implementation of Weibo system based on SSM
Laravel artisan common commands
MySQL view concept, create view, view, modify view, delete view
[rust web rokcet Series 1] Hello, world and get, post, put, delete
正则表达式学习笔记
卷積神經網絡(包含代碼與相應圖解)
The difference between new and malloc
5g/4g pole gateway_ Smart pole gateway
This is the form of the K-line diagram (pithy formula)
Redis环境搭建和使用的方法
Based on configured schedule, the given trigger will never fire
JMeter (II) - install the custom thread groups plug-in
Should enterprises choose server free computing?