From db6325632de1790c86125d55d7cd96122a1c9cc1 Mon Sep 17 00:00:00 2001 From: Gaurav Rawat <104276743+GauravRawat369@users.noreply.github.com> Date: Sat, 1 Oct 2022 23:57:22 +0530 Subject: [PATCH] spiral matrix --- Spiral Matrix.cpp | 79 ++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/Spiral Matrix.cpp b/Spiral Matrix.cpp index 432807b..d56cc19 100644 --- a/Spiral Matrix.cpp +++ b/Spiral Matrix.cpp @@ -1,43 +1,50 @@ -lass Solution{ - - public: - int findK(vector> &matrix, int n, int m, int k) - { - // Your code goes here - int temp=m; - m=n; - n=temp; +class Solution { +public: + vector spiralOrder(vector>& matrix) { - int r=0; - int c=0; - int count=0; - vectorans(m*n); + vectorvar; + int row=matrix.size(); + int col=matrix[0].size(); - while(r=c;i--){ - ans[count++]=matrix[m-1][i]; - } - m--; + ending_col--; + //ending_row + for(int i=ending_col;count=starting_col;i--) + { + var.push_back(matrix[ending_row][i]); + count++; } - if(c=r;i--){ - ans[count++]=matrix[i][c]; - } - c++; - } + ending_row--; + //staring col + for(int i=ending_row;count=starting_row;i--) + { + var.push_back(matrix[i][starting_col]); + count++; + } + starting_col++; } - - return ans[k-1]; + return var; } - -}; \ No newline at end of file +};