当前位置:网站首页>剑指 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;
}
}
边栏推荐
- Altium designer measure distance (ctrl+m)
- Medical management system (C language course for freshmen)
- MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
- Learn about servlets
- Android: the kotlin language uses grendao3, a cross platform app development framework
- 如何远程、在线调试app?
- 【LeetCode 43】236. The nearest common ancestor of binary tree
- matlab 使用 audioread 、 sound 读取和播放 wav 文件
- The technology boss is ready, and the topic of position C is up to you
- Laravel artisan 常用命令
猜你喜欢

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

Cross domain? Homology? Understand what is cross domain at once

Software No.1

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable

Four basic strategies for migrating cloud computing workloads

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

【LeetCode 43】236. The nearest common ancestor of binary tree

城市选择器组件实现原理

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

MySQL主从延迟问题怎么解决
随机推荐
Word search applet design report based on cloud development +ppt+ project source code + demonstration video
Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
Self drawing of menu items and CListBox items
Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
How to use a product to promote "brand thrill"?
734. Energy stone (greed, backpack)
II Basic structure of radio energy transmission system
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
The technology boss is ready, and the topic of position C is up to you
自动浏览拼多多商品
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
Based on configured schedule, the given trigger will never fire
This is the form of the K-line diagram (pithy formula)
There are spaces in the for loop variable in the shell -- IFS variable
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
Electronic Society C language level 1 32, calculate the power of 2
Redis有序集合如何使用
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
[Maya] the error of importing Maya into Metahuman
The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu