当前位置:网站首页>Leetcode interview question 29 clockwise print matrix
Leetcode interview question 29 clockwise print matrix
2022-06-26 18:13:00 【Maccy37】
Code carrier is me !
Reference resources LeetCode The website links :https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/solution/shun-shi-zhen-da-yin-ju-zhen-by-leetcode-solution/
subject : Enter a matrix , Print each number in clockwise order from the outside to the inside .
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]
Limit :
0 <= matrix.length <= 1000 <= matrix[i].length <= 100
I saw 《 The finger of the sword offer》 The problem-solving inside still doesn't understand his code
How to solve the problem : Layer by layer simulation
You can think of a matrix as layers , First print the outermost element , Second, print the elements of the outer layer , Until the innermost element is printed .
Define the order of a matrix k The distance from the layer to the nearest boundary is kk All the vertices of . for example , The outermost elements of the lower matrix are all the 1 layer , The sub outer elements are the first 2 layer , The rest of the elements are the first 3 layer .
[1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 2, 2, 1],
[1, 2, 3, 3, 3, 2, 1],
[1, 2, 2, 2, 2, 2, 1],
[1, 1, 1, 1, 1, 1, 1]
For each layer , Start at the top left and go through all the elements in a clockwise order . Suppose that the upper left corner of the current layer is located in (top,left), The lower right corner is located in (bottom,right), Traverse the elements of the current layer in the following order .
1. Traverse the upper element from left to right , In turn (top,left) To (top,right).
2. Traverse the right element from top to bottom , In turn (top+1,right) To (bottom,right)
3. If left<right And top<bottom, Then traverse the lower element from right to left , In turn (bottom,right-1) To (bottom,left+1), And traverse the left element from bottom to top , In turn (bottom,left) To (top+1,left)

class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if(matrix.size()<=0||matrix[0].size()<=0)
return {};
int rows=matrix.size(),columns=matrix[0].size();
int left=0,right=columns-1,top=0,bottom=rows-1;
vector<int>order;
while(left<=right&&top<=bottom)
{
for(int column=left;column<=right;column++)
{
order.push_back(matrix[top][column]);
}
for(int row=top+1;row<=bottom;row++)
{
order.push_back(matrix[row][right]);
}
if(left<right&&top<bottom)
{
for(int column=right-1;column>left;column--)
{
order.push_back(matrix[bottom][column]);
}
for(int row=bottom;row>top;row--)
{
order.push_back(matrix[row][left]);
}
}
left++;
right--;
top++;
bottom--;
}
return order;
}
};It is easier to understand this way of thinking .
边栏推荐
- Ethereum技术架构介绍
- JS common regular expressions
- 无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!
- Tencent qianzhiming: Exploration and application of pre training methods in information flow business
- ROS查询话题具体内容常用指令
- RSA concept explanation and tool recommendation - LMN
- 新手炒股开户选哪个证券公司比较好?怎样炒股比较安全??
- 小程序设置按钮分享功能
- KDD 2022 | how to use comparative learning in cross domain recommendation?
- Paging query and join Association query optimization
猜你喜欢

让torch.cuda.is_available()从false变成true的一点经验

DoS及攻击方法详解

Boyun, standing at the forefront of China's container industry

Get and set settings in 26class

数字签名论述及生成与优点分析

wechat_ Solve the problem of page Jump and parameter transfer by navigator in wechat applet

Bayesian network explanation

RSA concept explanation and tool recommendation - LMN

ISO文件

In and exceptions, count (*) query optimization
随机推荐
RSA concept explanation and tool recommendation - LMN
RSA概念详解及工具推荐大全 - lmn
JNI的 静态注册与动态注册
让torch.cuda.is_available()从false变成true的一点经验
非对称密码体制详解
分页查询、JOIN关联查询优化
In and exceptions, count (*) query optimization
Vscode usage - Remote SSH configuration description
输入n个整数,输出出现次数大于等于数组长度一半的数
Dos et détails de la méthode d'attaque
pycharm如何修改多行注释快捷键
同花顺开户怎么样安全吗?怎么炒股开户
KDD 2022 | 如何在跨域推荐中使用对比学习?
A little experience of next (ITER (dataloader))
数字签名标准(DSS)
将字符串B插入字符串A,有多少种插入办法可以使新串是一个回文串
Lm06 the mystery of constructing the bottom and top trading strategy only by trading volume
Li Kou daily question - day 28 -566 Reshape matrix
ros::spinOnce()和ros::spin()的使用和区别
利用递归实现求n位所有格雷码