LeetCode :Spiral Matrix
Problem:
https://leetcode.com/problems/spiral-matrix/#include<bits/stdc++.h> using namespace std; class Solution { public: vector<int> spiralOrder(vector<vector<int> > &matrix) { vector<int>v; if(matrix.size()==0) return v; int t=0,b=matrix.size()-1,l=0,r=matrix[0].size()-1; while(1) { for(int i=t;i<=r;i++) v.push_back(matrix[t][i]);t++; if(l>r || t>b) break; for(int i=t;i<=b;i++) v.push_back(matrix[i][r]);r--; if(l>r || t>b) break; for(int i=r;i>=l;i--) v.push_back(matrix[b][i]);b--; if(l>r || t>b) break; for(int i=b;i>=t;i--) v.push_back(matrix[i][l]);l++; if(l>r || t>b) break; } return v; } };
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment