当前位置:网站首页>54. Spiral matrix & 59 Spiral matrix II ●●
54. Spiral matrix & 59 Spiral matrix II ●●
2022-07-05 04:50:00 【chenyfan_】
54. Spiral matrix ●●
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 .
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]
- simulation
The four edges traverse in order , Move 、 Judgement boundary .
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size(); // m That's ok
int n = matrix[0].size(); // n Column
int top = 0, right = n-1, left = 0, buttom = m-1; // Boundary index value
int numsize = m*n;
int num = 0;
vector<int> ans(numsize);
while(true){
for(int i = left; i <= right; i++){
ans[num++] = matrix[top][i];
}
top++;
if(top>buttom) break; // Traverse Judgement boundary
for(int i = top; i <= buttom; i++){
ans[num++] = matrix[i][right];
}
right--;
if(left>right) break;
for(int i = right; i >= left; i--){
ans[num++] = matrix[buttom][i];
}
buttom--;
if(top>buttom) break;
for(int i = buttom; i >= top; i--){
ans[num++] = matrix[i][left];
}
left++;
if(left>right) break;
}
return ans;
}
};
59. Spiral matrix II ●●
Give you a positive integer n , Generate a include 1 To n 2 n^2 n2 All the elements , And the element press Clockwise order Spirally arranged n x n square matrix matrix .
Input :n = 3
Output :[[1,2,3],[8,9,4],[7,6,5]]
- Generate a n×n Empty matrix ans, Then simulate the whole inward surrounding filling process :
- Define the current left and right upper and lower boundaries l,r,t,b, Initial value num = 1, Iteration termination value numsize = n * n;
- When num <= numsize when , Always follow From left to right From top to bottom From right to left From bottom to top Fill in the sequence loop , After each filling :
- perform num += 1: Get the next number to fill in ;
- Update boundaries : For example, after filling in from left to right , Upper boundary t += 1, Equivalent to the upper boundary shrinking inward 1.
- Use num <= numsize instead of l < r || t < b As an iterative condition , In order to Solve when n In an odd number of , The problem that the central number of the matrix cannot be filled in during the iteration .
- Eventually return ans that will do .
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n, 0));
int up = 0, down = n-1, left = 0, right = n-1;
int cnt = 1, num = n*n;
while(cnt <= num){
// Iteration conditions , Number of fillings <= Lattice number
for(int i = left; i <= right; ++i) ans[up][i] = cnt++; // Move right
++up;
for(int i = up; i <= down; ++i) ans[i][right] = cnt++; // Move down
--right;
for(int i = right; i >= left; --i) ans[down][i] = cnt++; // Move left
--down;
for(int i = down; i >= up; --i) ans[i][left] = cnt++; // Move upward
++left;
}
return ans;
}
};
边栏推荐
- mysql审计日志归档
- Rip notes [rip message security authentication, increase of rip interface measurement]
- On-off and on-off of quality system construction
- 中国溶聚丁苯橡胶(SSBR)行业研究与预测报告(2022版)
- [ideas] 2021 may day mathematical modeling competition / May Day mathematical modeling ideas + references + codes
- MySQL audit log Archive
- "Measuring curve length" of CAD dream drawing
- Data security -- 14 -- Analysis of privacy protection governance
- 2022-2028 global and Chinese equipment as a Service Market Research Report
- Neural networks and deep learning Chapter 6: Circular neural networks reading questions
猜你喜欢
AutoCAD - scaling
Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
2022 thinking of mathematical modeling a problem of American college students / analysis of 2022 American competition a problem
AutoCAD - full screen display
Download the details and sequence of the original data access from the ENA database in EBI
Observable time series data downsampling practice in Prometheus
【acwing】837. Number of connected block points
XSS injection
Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
2022 thinking of mathematical modeling D problem of American college students / analysis of 2022 American competition D problem
随机推荐
windows下Redis-cluster集群搭建
【acwing】836. Merge sets
Manually implement heap sorting -838 Heap sort
Key review route of probability theory and mathematical statistics examination
Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
質量體系建設之路的分分合合
2022-2028 global and Chinese video coding and transcoding Market Research Report
History of web page requests
【Leetcode】1352. 最后 K 个数的乘积
[crampon game] MC tutorial - first day of survival
2021 electrician cup idea + code - photovoltaic building integration plate index development trend analysis and prediction: prediction planning issues
Wenet: E2E speech recognition tool for industrial implementation
Neural networks and deep learning Chapter 3: linear model reading questions
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
#775 Div.1 C. Tyler and Strings 组合数学
CSDN body auto generate directory
Introduce Hamming distance and calculation examples
[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)
Detailed introduction of OSPF header message