无聊系列 - 螺旋矩阵算法实现

前段时间无意中看到一个螺旋矩阵的面试题,于是网上搜了下,发现有两种螺旋矩阵:

1. 百度百科上描述的这种:https://baike.baidu.com/item/%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/3528521?fr=aladdin

2. leet code上的螺旋矩阵输出。

先贴百科上的:

 1 public class SpiralMatrix {
 2     public static void main(String[] args) {
 3         int n = 11, m = 7;
 4 
 5         int[][] elements = new int[n][m];
 6 
 7         int temp = 0;
 8         int row = 0, col = 0;
 9         int maxN = n, maxM = m;
10 
11         while (temp < n * m) {
12 
13             int tempRow = row, tempCol = col;
14             // top
15             for (; tempCol < maxM; tempCol++) {
16                 temp++;
17                 elements[tempRow][tempCol] = temp;
18             }
19 
20             if (temp >= n * m)
21                 break;
22 
23             // right
24             tempCol--;
25             for (tempRow = tempRow + 1; tempRow < maxN; tempRow++) {
26                 temp++;
27                 elements[tempRow][tempCol] = temp;
28             }
29             if (temp >= n * m)
30                 break;
31             tempRow--;
32 
33             // bottom
34             tempCol--;
35             for (; tempCol >= m - maxM; tempCol--) {
36                 temp++;
37                 elements[tempRow][tempCol] = temp;
38             }
39             if (temp >= n * m)
40                 break;
41             // left
42             tempCol++;
43             for (tempRow = tempRow - 1; tempRow > n - maxN; tempRow--) {
44                 temp++;
45                 elements[tempRow][tempCol] = temp;
46             }
47 
48             if (temp >= n * m)
49                 break;
50 
51             row++;
52             col++;
53 
54             maxM--;
55             maxN--;
56         }
57 
58         for (int i = 0; i < n; i++) {
59             for (int j = 0; j < m; j++) {
60                 if (elements[i][j] < 10)
61                     System.out.print("0");
62 
63                 System.out.print(elements[i][j]);
64                 System.out.print("  ");
65             }
66             System.out.println();
67         }
68     }
69 }

【力扣算法】54-螺旋矩阵:

 1 public static List<Integer> spiralOrder(int[][] matrix) {
 2         List<Integer> result = new LinkedList<>();
 3         
 4         if (matrix.length == 0)
 5             return result;        
 6 
 7         int maxRow = matrix.length;
 8         int maxCol = matrix[0].length;
 9 
10         int total = maxRow * maxCol;
11 
12         // 统计已经读取了多少个数字
13         int num = 0;
14 
15         int row = 0, col = 0;
16 
17         while (num < total) {
18             int tempRow = row, tempCol = col;
19             // top
20             for (; tempCol < maxCol; tempCol++) {
21                 num++;
22                 result.add(matrix[tempRow][tempCol]);
23             }
24             // 读取的个数达到矩阵元素总数,则退出循环
25             if (num >= total)
26                 break;
27 
28             // right
29             tempCol--;
30             for (tempRow = tempRow + 1; tempRow < maxRow; tempRow++) {
31                 num++;
32                 result.add(matrix[tempRow][tempCol]);
33             }
34             tempRow--;
35 
36             if (num >= total)
37                 break;
38 
39             // bottom
40             tempCol--;
41             for (; tempCol >= col; tempCol--) {
42                 num++;
43                 result.add(matrix[tempRow][tempCol]);
44             }
45             if (num >= total)
46                 break;
47 
48             // left
49             tempCol++;
50             for (tempRow = tempRow - 1; tempRow > row; tempRow--) {
51                 num++;
52                 result.add(matrix[tempRow][tempCol]);
53             }
54 
55             if (num >= total)
56                 break;
57 
58             row++;
59             col++;
60 
61             maxCol--;
62             maxRow--;
63         }
64 
65         return result;
66     }
67 
68     public static void main(String[] args) {
69         int[][] matrix = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
70 
71         matrix = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
72 
73         List<Integer> result = spiralOrder(matrix);
74 
75         for (Integer integer : result) {
76             System.out.print(integer);
77             System.out.print("  ");
78         }
79     }

【力扣算法】54-螺旋矩阵

猜你喜欢

转载自www.cnblogs.com/chongsha/p/11946349.html
今日推荐