问题描述
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
边界索引
依次遍历外围的数值,每次遍历一行或者一列之后,需要调整边界大小(up,down,right,left四个边),并且判断是否已经遍历完成(当up>down,left>right说明要超出边界了,已经遍历完成所有)。
C++
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if(matrix.empty()||matrix[0]