In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to realize spiral matrix in C++." Xiaobian shows you the operation process through actual cases. The operation method is simple and fast, and the practicability is strong. I hope this article "how to realize spiral matrix in C++" can help you solve the problem.
Spiral Matrix II
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
Example:
Input: 3
Output:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
This problem is essentially no different from the previous Spiral Matrix, which is equivalent to a process similar to inverse operation. This problem is to fill the numbers in the order of spiral. Since the given rectangle is a square, we use n / 2 to calculate the number of rings. If n is odd, the middle point is not counted in the number of rings, so we need to assign values separately. Or is the subscript conversion problem difficult? Refer to the previous explanation of Spiral Matrix to convert subscripts. See code below:
Solution 1:
class Solution {public: vector generateMatrix(int n) { vector res(n, vector(n)); int val = 1, p = n; for (int i = 0; i
< n / 2; ++i, p -= 2) { for (int col = i; col < i + p; ++col) res[i][col] = val++; for (int row = i + 1; row < i + p; ++row) res[row][i + p - 1] = val++; for (int col = i + p - 2; col >= i; --col) res[i + p - 1][col] = val++; for (int row = i + p - 2; row > i; --row) res[row][i] = val++; } if (n % 2 != 0) res[n / 2][n / 2] = val; return res; }};
Of course, we can also use the following simplified coordinate conversion method, bloggers personally prefer the following solution, not easy to make mistakes, and easy to understand, see the code as follows:
Solution 2:
class Solution {public: vector generateMatrix(int n) { vector res(n, vector(n)); int up = 0, down = n - 1, left = 0, right = n - 1, val = 1; while (true) { for (int j = left; j down) break; for (int i = up; i = left; --j) res[down][j] = val++; if (--down
< up) break; for (int i = down; i >= up; --i) res[i][left] = val++; if (++left > right) break; } return res; }}; About "C++ how to implement spiral matrix" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.