二、模拟
-
剑指29,顺时针打印矩阵
//时间O(mn),空间O(mn) //模拟,定义四个边界,每遍历一行就收缩一次边界,同时注意跳出循环边界条件 class Solution { public: vector<int> spiralOrder(vector<vector<int>>& vec) { if(vec.empty()) return {}; vector<int> res; int left = 0; int top = 0; int right = vec[0].size()-1; int bottom = vec.size()-1; while(1) { //1 for(int i=left;i<=right;++i) { res.push_back(vec[top][i]); } if(++top > bottom) break; //2 for(int i=top;i<=bottom;++i) { res.push_back(v