当前位置:网站首页>LeetCode Algorithm 2326. Spiral Matrix IV
LeetCode Algorithm 2326. Spiral Matrix IV
2022-07-30 04:45:00 【Alex_996】
题目链接:2326. 螺旋矩阵 IV
Ideas
算法:边界压缩
数据结构:链表
思路:It seems that I have encountered similar problems when I first started learning algorithms,What rotates to print a matrix or something like that,similar to this.
- 首先创建一个 m * n 的矩阵,默认用 -1 填充即可;
- 定义up、down、left、right四个变量,represent the four boundaries of the matrix, respectively;
- Traverse the linked list to fill in the matrix,After completing a row or column, the corresponding boundary is compressed,The order in which rows and columns are traversed is clockwise.
Code
C++
class Solution {
public:
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
vector<vector<int>> res(m, vector<int>(n, -1));
int up = 0, down = m - 1, left = 0, right = n - 1;
while (head) {
// 1.从左向右,行不变列变
for (int i = left; i < right + 1; i++) {
res[up][i] = head->val;
head = head->next;
if (head == nullptr) return res;
}
if (up < down) up++;
// 2.从上到下,列不变行变
for (int i = up; i < down + 1; i++) {
res[i][right] = head->val;
head = head->next;
if (head == nullptr) return res;
}
if (left < right) right--;
// 3.从右向左,行不变列变
for (int i = right; i > left - 1; i--) {
res[down][i] = head->val;
head = head->next;
if (head == nullptr) return res;
}
if (up < down) down--;
// 4.从下到上,列不变行变
for (int i = down; i > up - 1; i--) {
res[i][left] = head->val;
head = head->next;
if (head == nullptr) return res;
}
if (left < right) left++;
}
return res;
}
};
边栏推荐
- 五、视图解析与模板引擎
- Discourse Custom Header Links
- Weight line segment tree + line segment tree split/merge + CF1659D
- Become a qualified cybersecurity, do you know this?
- Go 学习笔记(84)— Go 项目目录结构
- 2021 Shandong Province Network Construction and Application Test Questions
- 双指针问题(中)
- 05 Detailed explanation of the global configuration file application.properties
- String Problem (Part 1)
- labelme的使用技巧
猜你喜欢

Unity beginner 5 cameras follow, border control and simple particle control (2 d)

BGP的简单实验

Perspective transformation matrix of image perspective correction should be matrix (single)/findHomography with getPerspectiveTransformd difference

DAY17: weak password detection and test

KubeMeet Registration | The complete agenda of the "Edge Native" Online Technology Salon has been announced!

Alibaba Cloud's EasyNLP Chinese text image generation model takes you to become an artist in seconds

The Complete Go Books - Beginner to Advanced and Web Development

Image stitching (registration) case based on OpenCV

Xiamen SenseCore Technology MC3172(1): Introduction and Environment Construction

Dynamic Programming Problems (End)
随机推荐
WPF introduces ttf icon file usage record
cnpm installation steps
2.6基数排序(桶排序)
Discourse 自定义头部链接(Custom Header Links)
[MRCTF2020]Hello_ misc
[Awards every week] The "Edge Containers" track of the Cloud Native Programming Challenge invites you to fight!
1315_使用LOOPBACK模拟模式pyserial安装是否成功的测试
Six, read application configuration + log configuration
Boss Rush (two-point answer + DP)
Notes on "The Law of Construction"---Chapter 10 Typical Users and Scenarios
山西省第二届网络安全技能大赛(企业组)部分赛题WP(七)
2.6 Merge Sort
文件系统二
error: The following untracked working tree files would be overwritten by
SaaS多租户数据隔离的三种解决方案
2.5 Quick Sort
解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b
Alibaba Cloud's EasyNLP Chinese text image generation model takes you to become an artist in seconds
sql statement - how to query data in another table based on the data in one table
Solve the error SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb7 in position 0: invalid start b