|
1 |
| -package com.thealgorithms.others; |
2 |
| - |
3 |
| -import java.util.ArrayList; |
4 |
| -import java.util.List; |
5 |
| - |
6 |
| -public class PrintAMatrixInSpiralOrder { |
7 |
| - /** |
8 |
| - * Search a key in row and column wise sorted matrix |
9 |
| - * |
10 |
| - * @param matrix matrix to be searched |
11 |
| - * @param row number of rows matrix has |
12 |
| - * @param col number of columns matrix has |
13 |
| - * @author Sadiul Hakim : https://github.com/sadiul-hakim |
14 |
| - */ |
15 |
| - |
16 |
| - public List<Integer> print(int[][] matrix, int row, int col) { |
17 |
| - |
18 |
| - // r traverses matrix row wise from first |
19 |
| - int r = 0; |
20 |
| - // c traverses matrix column wise from first |
21 |
| - int c = 0; |
22 |
| - int i; |
23 |
| - |
24 |
| - List<Integer> result = new ArrayList<>(); |
25 |
| - |
26 |
| - while (r < row && c < col) { |
27 |
| - // print first row of matrix |
28 |
| - for (i = c; i < col; i++) { |
29 |
| - result.add(matrix[r][i]); |
30 |
| - } |
31 |
| - |
32 |
| - // increase r by one because first row printed |
33 |
| - r++; |
34 |
| - |
35 |
| - // print last column |
36 |
| - for (i = r; i < row; i++) { |
37 |
| - result.add(matrix[i][col - 1]); |
38 |
| - } |
39 |
| - |
40 |
| - // decrease col by one because last column has been printed |
41 |
| - col--; |
42 |
| - |
43 |
| - // print rows from last except printed elements |
44 |
| - if (r < row) { |
45 |
| - for (i = col - 1; i >= c; i--) { |
46 |
| - result.add(matrix[row - 1][i]); |
47 |
| - } |
48 |
| - |
49 |
| - row--; |
50 |
| - } |
51 |
| - |
52 |
| - // print columns from first except printed elements |
53 |
| - if (c < col) { |
54 |
| - for (i = row - 1; i >= r; i--) { |
55 |
| - result.add(matrix[i][c]); |
56 |
| - } |
57 |
| - c++; |
58 |
| - } |
59 |
| - } |
60 |
| - return result; |
61 |
| - } |
62 |
| -} |
| 1 | +package com.thealgorithms.matrix; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +public class PrintAMatrixInSpiralOrder { |
| 7 | + /** |
| 8 | + * Search a key in row and column wise sorted matrix |
| 9 | + * |
| 10 | + * @param matrix matrix to be searched |
| 11 | + * @param row number of rows matrix has |
| 12 | + * @param col number of columns matrix has |
| 13 | + * @author Sadiul Hakim : https://github.com/sadiul-hakim |
| 14 | + */ |
| 15 | + |
| 16 | + public List<Integer> print(int[][] matrix, int row, int col) { |
| 17 | + |
| 18 | + // r traverses matrix row wise from first |
| 19 | + int r = 0; |
| 20 | + // c traverses matrix column wise from first |
| 21 | + int c = 0; |
| 22 | + int i; |
| 23 | + |
| 24 | + List<Integer> result = new ArrayList<>(); |
| 25 | + |
| 26 | + while (r < row && c < col) { |
| 27 | + // print first row of matrix |
| 28 | + for (i = c; i < col; i++) { |
| 29 | + result.add(matrix[r][i]); |
| 30 | + } |
| 31 | + |
| 32 | + // increase r by one because first row printed |
| 33 | + r++; |
| 34 | + |
| 35 | + // print last column |
| 36 | + for (i = r; i < row; i++) { |
| 37 | + result.add(matrix[i][col - 1]); |
| 38 | + } |
| 39 | + |
| 40 | + // decrease col by one because last column has been printed |
| 41 | + col--; |
| 42 | + |
| 43 | + // print rows from last except printed elements |
| 44 | + if (r < row) { |
| 45 | + for (i = col - 1; i >= c; i--) { |
| 46 | + result.add(matrix[row - 1][i]); |
| 47 | + } |
| 48 | + |
| 49 | + row--; |
| 50 | + } |
| 51 | + |
| 52 | + // print columns from first except printed elements |
| 53 | + if (c < col) { |
| 54 | + for (i = row - 1; i >= r; i--) { |
| 55 | + result.add(matrix[i][c]); |
| 56 | + } |
| 57 | + c++; |
| 58 | + } |
| 59 | + } |
| 60 | + return result; |
| 61 | + } |
| 62 | +} |
0 commit comments