Advertisement
VaishanviShri

spiral array

Sep 9th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. #include <vector>
  2. vector<int> Solution::spiralOrder(const vector<vector<int> > &A) {
  3. vector<int> soln;
  4. soln.push_back(A[0][0]);
  5. int i,j;
  6. int r = A.size() - 1;
  7. int c = A[0].size() - 1;
  8. for(i=0; i<c; i++){
  9. for(j=i+1; j<=(c-i); j++)
  10. soln.push_back(A[i][j]);
  11. for(j=i+1;j<=r-i;j++);
  12. soln.push_back(A[j][c-i]);
  13. for(j=(c-i-1);j>=i;j--)
  14. soln.push_back(A[r-i][j]);
  15. for(j=(r-i-1);j>=i;j--)
  16. soln.push_back(A[j][i]);
  17. }
  18. return soln;
  19.  
  20. }
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement