Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> spiralOrder(vector<vector<int>>& m) {
- vector<int> res;
- int rows = m.size(), cols = m[0].size();
- int left=0, right=cols-1, up=0, down=rows-1;
- while(res.size() < rows*cols){
- for(int col=left; col<=right; col++)
- res.push_back(m[up][col]);
- for(int row=up+1; row<=down; row++)
- res.push_back(m[row][right]);
- if(up != down){
- for(int col=right-1; col>=left; col--)
- res.push_back(m[down][col]);
- }
- if(left != right){
- for(int row=down-1; row>up; row--){
- res.push_back(m[row][left]);
- }
- }
- left++; right--; up++; down--;
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement