当前位置:网站首页>[leetcode 54] spiral matrix
[leetcode 54] spiral matrix
2022-04-23 06:24:00 【Don't steal my energy】
Title Description
To give you one m That's ok n Columns of the matrix matrix , Please follow Clockwise spiral sequence , Returns all elements in the matrix .
Example 1 :
Input :matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output :[1,2,3,6,9,8,7,4,5]
Example 2 :
Input :matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output :[1,2,3,4,8,12,11,10,9,5,6,7]
Ideas
Separate use up、down、left、right To represent the boundary of the matrix
The order of traversal
1. Left to right matrix[up][left]....matrix[up][right]
2. Up and down matrix[up+1][right]....matrix[down][right] // Be careful up+1 For the indentation of the boundary
3. Right to left matrix[down][right-1]....matrix[down][left] // Be careful right-1 For the indentation of the boundary
4. Down to up matrix[down+1][left]....matrix[up][left] //down-1 ditto
The main difficulty of this problem is how to determine the four boundaries , When initializing ,up Namely 0,down yes x_len-1,left yes 0,right yes y_len-1. Enter... In the above traversal order while loop , After each traversal, you need to Boundary indent and Boundary judgment , if left>right perhaps up>down when , Indicates the end of the traversal , direct break end . The core of the following code is that after each operation ( Left to right 、 Up and down 、 Right to left and bottom to top ) after , Will indent and judge the boundary .
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ans;
int x_len=matrix.size();// Row number
int y_len=matrix[0].size();// Number of columns
// Mark the boundaries of all four points
int left=0,right=y_len-1,up=0,down=x_len-1;
while(true){
// From left to right assignment
for(int i=left;i<=right;i++)
ans.push_back(matrix[up][i]);
if(++up > down)// Indent the boundary up=up+1 operation Pay attention to ++up stay up==down It was all about up=up+1 Replacement of
break;
// From top to bottom At this time up The value of is compared to the previous for Medium up We've added 1
for(int i=up;i<=down;i++)
ans.push_back(matrix[i][right]);
if(--right < left)// Indent the boundary right=right-1 operation
break;
// From right to left Be careful right Change in value of
for(int i=right;i>=left;i--)
ans.push_back(matrix[down][i]);
if(--down < up)// Indent the boundary down=down-1 operation
break;
// From bottom to top Be careful down Change in value of
for(int i=down;i>=up;i--)
ans.push_back(matrix[i][left]);
if(++left > right)// Indent the boundary left=left+1 operation
break;
}
return ans;
}
Reference resources
Comment area Anatole Big man's answer
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617011766.html
边栏推荐
- Integers have friends interval GCD + double pointer
- Illustrate the significance of hashcode
- MySQL occasional Caton
- Installation and usage skills of idea
- C language file operation
- Pyqy5 learning (2): qmainwindow + QWidget + qlabel
- Practical operation - Nacos installation and configuration
- Programming training
- 自动控制原理知识点整合归纳(韩敏版)
- MySQL best practices for creating tables
猜你喜欢
Pytoch -- data loading and processing
Graphic numpy array matrix
You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
List segmentation best practices
Algèbre linéaire chapitre 1 - déterminants
深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
檢測技術與原理
Guaba and Computational Geometry
随机推荐
Exception handling: grab and throw model
6.Reversal
Common programming records - parser = argparse ArgumentParser()
4. Print form
A general U-shaped transformer for image restoration
Collections multiple parameter sorting
[leetcode 67] sum of two binary numbers
Use of multithreaded executors
The problem that the page will refresh automatically after clicking the submit button on the form is solved
12. Monkeys climb mountains
Numpy common function table sorting of data processing
PHP processing JSON_ Decode() parses JSON stringify
Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
Doomsday (simple computational geometry)
Kalman filter and inertial integrated navigation
A sharp tool to improve work efficiency
Detection technology and principle
Best practices for MySQL storage time
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
Calculation (enter the calculation formula to get the result)