当前位置:网站首页>剑指 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;
}
}
边栏推荐
- Parted command
- 正则表达式学习笔记
- This is the form of the K-line diagram (pithy formula)
- new和malloc的区别
- 电子协会 C语言 1级 33 、奇偶数判断
- MySQL约束与多表查询实例分析
- 企业应该选择无服务器计算吗?
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- 10 minutes to get started quickly composition API (setup syntax sugar writing method)
- SQLite 3 of embedded database
猜你喜欢

What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation

The concept, function, characteristics, creation and deletion of MySQL constraints

开发工具创新升级,鲲鹏推进计算产业“竹林”式生长

Three core problems of concurrent programming

matlab 使用 resample 完成重采样

【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享

How to debug apps remotely and online?

leetcode2312. 卖木头块(困难,周赛)

Memorabilia of domestic database in June 2022

Matlab uses resample to complete resampling
随机推荐
牛客网——华为题库(51~60)
Using tabbar in wechat applet
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
Openssl3.0 learning XXI provider encoder
Construction and maintenance of business websites [14]
正则表达式学习笔记
Construction and maintenance of business websites [10]
TSINGSEE青犀平台如何实现同一节点同时播放多个视频?
How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
ES6 new method of string
分卷压缩,解压
1069. Division of convex polygons (thinking, interval DP)
机器学习基本概念
Penser au jeu 15: penser au service complet et au sous - service
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
The difference between new and malloc
Makefile simple induction