当前位置: 首页> 科技> 数码 > 网站超级推广_c2c模式平台有哪些_seo如何优化关键词_百度企业号

网站超级推广_c2c模式平台有哪些_seo如何优化关键词_百度企业号

时间:2025/7/8 15:22:06来源:https://blog.csdn.net/qq_38412266/article/details/144677327 浏览次数:0次
网站超级推广_c2c模式平台有哪些_seo如何优化关键词_百度企业号

1. 题目描述

给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

示例 2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

2. 解题思路

首先定义左指针left、右指针right,上指针top,下指针bottom,然后从左到右,从上到下遍历,遇到边界的时候指针相应地变化。

3. 代码实现

class Solution {
public:vector<int> spiralOrder(vector<vector<int>>& matrix) {int left = 0;int top = 0;int right = matrix[0].size()-1;int bottom = matrix.size()-1;vector<int> ans;while(left <= right || top <= bottom){for (int i = left; i <= right; i++){ans.push_back(matrix[top][i]);}top++;if (top > bottom) break;for (int i = top; i <= bottom; i++){ans.push_back(matrix[i][right]);}right--;if (right < left) break;for (int i = right; i >= left; i--){ans.push_back(matrix[bottom][i]);}bottom--;if (bottom < top) break;for (int i = bottom; i >= top; i--){ans.push_back(matrix[i][left]);}left++;if (left > right) break;}return ans;}
};
关键字:网站超级推广_c2c模式平台有哪些_seo如何优化关键词_百度企业号

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: