-
Notifications
You must be signed in to change notification settings - Fork 382
Closed
Description
LeetCode Username
karanpandey7211
Problem Number, Title, and Link
https://leetcode.com/problems/spiral-matrix/
Bug Category
Problem description
Bug Description
This is the problem from leetcode which is frequently asked by top MNC'S .
Language Used for Code
None
Code used for Submit/Run operation
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
int start_row=0;
int start_col=0;
int end_row=matrix.length-1;
int end_col=matrix[0].length-1;
while(start_row<=end_row && start_col<=end_col){
//printing the top boundary of the matrix.
for(int j=start_col;j<=end_col;j++){
result.add(matrix[start_row][j]);
}
//printing the right boundary of the matrix.
for(int i=start_row+1;i<=end_row;i++){
result.add(matrix[i][end_col]);
}
//printing the bottom boundary of the matrix.
for(int j=end_col-1;j>=start_col;j--){
if(start_row==end_row){
break;
}
result.add(matrix[end_row][j]);
}
//printing the left boundary of the matrix.
for(int i=end_row-1;i>=start_row+1;i--){
if(start_col==end_col){
break;
}
result.add(matrix[i][start_col]);
}
start_row++;
start_col++;
end_row--;
end_col--;
}
return result;
}
public static void main(String args[]){
Solution obj=new Solution();
int matrix[][]={{1,2,3},{4,5,6},{7,8,9}};
List<Integer> spiral = obj.spiralOrder(matrix);
System.out.println("Spiral Order: " + spiral);
}
}Expected behavior
in this matrix we used the concept of spiral in which first we print the outer boundary of the loop then inner bounday loop using some basics concept.
Screenshots
No response
Additional context
No response
Metadata
Metadata
Assignees
Labels
No labels