From 55f4bf067a81083390dcdfc8210ce89189f71a77 Mon Sep 17 00:00:00 2001 From: Shivaraj <1MS22IS127@msrit.edu> Date: Sun, 8 Dec 2024 18:11:50 +0530 Subject: [PATCH] Created Spiral_Matrix code from leetcode --- Spiral_Matrix.java | 108 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 Spiral_Matrix.java diff --git a/Spiral_Matrix.java b/Spiral_Matrix.java new file mode 100644 index 00000000..0c953cad --- /dev/null +++ b/Spiral_Matrix.java @@ -0,0 +1,108 @@ +class Spiral_Matrix { + public static void main(String[] args){ + int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}}; + Spiral_Matrix ob = new Spiral_Matrix(); + System.out.println(ob.spiralOrder(matrix)); + } + public List spiralOrder(int[][] matrix) { + ArrayList ans = new ArrayList<>(); + boolean[][] visited = new boolean[matrix.length][matrix[0].length]; + int count = 0; + int row = 0; + int col = 0; + boolean right = true; + boolean left = false; + boolean down = false; + boolean up = false; + boolean curr = right; + while(count!=(matrix.length*matrix[0].length)){ + if(!valid(row,col,matrix)){ + if(curr==right){ + col--; + row++; + down = true; + right = false; + curr = down; + } + else if(curr==left){ + col++; + row--; + left = false; + up = true; + curr = up; + } + else if(curr==down){ + col--; + row--; + down = false; + left = true; + curr = left; + } + else if(curr==up){ + col++; + row--; + up = false; + right = true; + curr = right; + } + } + if(!visited(row,col,visited)){ + visited[row][col] = true; + ans.add(matrix[row][col]); + count++; + if(right){ + col++; + } + else if(left){ + col--; + } + else if(down){ + row++; + } + else if(up){ + row--; + } + } + else{ + if(curr==right){ + col--; + row++; + down = true; + right = false; + curr = down; + } + else if(curr==left){ + col++; + row--; + left = false; + up = true; + curr = up; + } + else if(curr==down){ + col--; + row--; + down = false; + left = true; + curr = left; + } + else if(curr==up){ + col++; + row++; + up = false; + right = true; + curr = right; + } + } + + } + return ans; + } + private boolean valid(int r, int c, int[][] matrix){ + boolean row = (r>=0)&&(r=0)&&(c