From 5a8afb332c2500897e52f35c772ac91489b7e4d4 Mon Sep 17 00:00:00 2001 From: Pooja Malani <63039519+poojamalani15@users.noreply.github.com> Date: Fri, 8 Oct 2021 20:50:44 +0530 Subject: [PATCH] Create 54.cpp --- solutions/54.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 solutions/54.cpp diff --git a/solutions/54.cpp b/solutions/54.cpp new file mode 100644 index 0000000..891b8ea --- /dev/null +++ b/solutions/54.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + vector spiralOrder(vector>& matrix) { + int top=0,bottom=matrix.size()-1,left=0,right=matrix[0].size()-1; + vectorv; + while( left<=right && top<=bottom){ + for(int i=left;i<=right;i++) + v.push_back(matrix[top][i]); + top++; + for(int i=top;i<=bottom;i++) + v.push_back(matrix[i][right]); + right--; + if(top<=bottom){ + for(int i=right;i>=left;i--) + v.push_back(matrix[bottom][i]); + bottom--; + } + if(left<=right){ + for(int i=bottom;i>=top;i--) + v.push_back(matrix[i][left]); + left++; + } + } + return v; + } +};