Spiral Matrix Leetcode Solution
Spiral Matrix
Given an m x n matrix, return all elements of the matrix in spiral order.
Matrix is a set of numbers arranged in rows and columns so as to form a rectangular array. The numbers are called the elements, or entries, of the matrix.
Spiral Matrix Leetcode Solution :
Constraints :
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 10
- -100 <= matrix[i][j] <= 100
Example 1:
- Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
- Output: [1,2,3,4,8,12,11,10,9,5,6,7]
Approach :
This approach is not in editorial page but much better than those:
Control the corners as we circle through the whole matrix.
We can store the indices of the corners and using ‘if’ conditions we can traverse in circle. Once we’re back to the element at [0][0], we can shrink the matrix by 1 by manipulating the corner indices.
I might have not done great job in naming the variables in the code. Here are the variables I used to make it easier to understand.
- r_min -> Index of the first row
- r_max -> Index of the last row
- c_min -> Index of the first column
- c_max -> Index of the last column
- r and c are the indices of current element to be returned.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Code :
class Solution { public: vector spiralOrder(vector<vector>& matrix) { int r_min = 0, c_min = 0; int r_max = matrix.size()-1, c_max = matrix[0].size()-1; vector result; for(int i=0, r = 0, c = 0; i<matrix.size()*matrix[0].size(); i++) { result.push_back(matrix[r][c]); if (r==r_min && c!=c_max) c++; else if(r!=r_max && c==c_max) r++; else if(r==r_max && c!=c_min) c--; else if(r!=r_min && c==c_min) r--; if(r==r_min && c==c_min) r++, c++, r_min++, c_min++, r_max--, c_max--; } return result; } };
class Solution { public ListspiralOrder(int[][] matrix) { List result = new ArrayList<>(); if (matrix == null || matrix.length == 0) { return result; } int rows = matrix.length, cols = matrix[0].length; int left = 0, right = cols-1, top = 0, bottom = rows-1; while (left <= right && top <= bottom) { for (int i = left; i <= right; i++) { result.add(matrix[top][i]); } top++; for (int i = top; i <= bottom; i++) { result.add(matrix[i][right]); } right--; if (top <= bottom) { for (int i = right; i >= left; i--) { result.add(matrix[bottom][i]); } bottom--; } if (left <= right) { for (int i = bottom; i >= top; i--) { result.add(matrix[i][left]); } left++; } } return result; } }
class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: if not matrix: return [] rows, cols = len(matrix), len(matrix[0]) top, bottom, left, right = 0, rows-1, 0, cols-1 result = [] while len(result) < rows * cols: for i in range(left, right+1): result.append(matrix[top][i]) top += 1 for i in range(top, bottom+1): result.append(matrix[i][right]) right -= 1 if top <= bottom: for i in range(right, left-1, -1): result.append(matrix[bottom][i]) bottom -= 1 if left <= right: for i in range(bottom, top-1, -1): result.append(matrix[i][left]) left += 1 return result
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment