leetCode 54. Spiral Matrix

Posted yfs123456

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetCode 54. Spiral Matrix相关的知识,希望对你有一定的参考价值。

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Example 1:

Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input:
[
  [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]

思路很简单,整个输出过程用rowLowerBound,rowUpperBound,colLowerBound,colUpperBound来控制边界。输出时要控制边界缩小,并且输出的内容要在边界内。
几种特殊情况:
     {1,2,3,4},
{5,6,7,8},
{9,10,11,12}

{1,2,3},
{4,5,6},
{7,8,9}

{1 , 2, 3, 4, 5},
{6, 7, 8 ,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}

{ 1, 2, 3, 4, 5, 6},
{ 7, 8, 9,10,11,12},
{13,14,15,16,17,18},
{19,20,21,22,23,24}

{6,9,7}

{1},
{2},
{3}

{1,2},
{3,4},
{5,6},
{7,8}


 1 class Solution {
 2     public List<Integer> spiralOrder(int[][] matrix) {
 3         int row, col;
 4         row = matrix.length;
 5         ArrayList<Integer> list = new ArrayList<>();
 6         if (row == 0)
 7             return list;
 8         col = matrix[0].length;
 9 
10         /*
11         只能在边界内循环
12          */
13         int rowLowerBound = 0, rowUpperBound = row-1;
14         int colLowerBound = 0, colUpperBound = col-1;
15         while (rowLowerBound <= rowUpperBound && colLowerBound <= colUpperBound){
16             /*
17             沿着行rowLowerBound从左至右输出
18             沿着列colUpperBound从上到右输出
19             沿着行rowUpperBound从右到左输出
20             沿着列colLowerBound从下到上输出
21 
22             变更边界条件(转向内层矩阵,等于是将原来矩阵剥了一层皮)
23             rowLowerBound+1, rowUpperBound-1, colLowerBound+1,colUpperBound-1
24              */
25             for (int i = colLowerBound, flag = 1; i < colUpperBound; i++) {//
26                 list.add(matrix[rowLowerBound][i]);
27                 if (rowLowerBound == rowUpperBound){
28                     ++colLowerBound;
29                 }else if(colLowerBound < colUpperBound && flag == 1 && rowUpperBound != row-1){
30                     ++colLowerBound;
31                     flag = 0;
32                 }
33             }
34             for (int i = rowLowerBound, flag = 1; i < ((rowUpperBound == 0) ? 1 : rowUpperBound); i++) {//
35                 list.add(matrix[i][colUpperBound]);
36 
37                 if (colLowerBound == colUpperBound){
38                     ++rowLowerBound;
39                 }else if(rowLowerBound < rowUpperBound && flag == 1) {
40                     ++rowLowerBound;
41                     flag = 0;
42                 }
43             }
44             for (int i = colUpperBound, flag = 1; i > colLowerBound; i--) {//
45                 list.add(matrix[rowUpperBound][i]);
46                 if(rowLowerBound == rowUpperBound)
47                     --colUpperBound;
48                 else if (colLowerBound < colUpperBound && flag == 1){
49                     --colUpperBound;
50                     flag = 0;
51                 }
52             }
53             for (int i = rowUpperBound, flag = 1; i > rowLowerBound; i--) {//
54                 list.add(matrix[i][colLowerBound]);
55                 if (colLowerBound == colUpperBound)
56                     --rowUpperBound;
57                 else if (rowLowerBound < rowUpperBound && flag == 1){
58                     --rowUpperBound;
59                     flag = 0;
60                 }
61             }
62             if (rowLowerBound == rowUpperBound && colLowerBound == colUpperBound){
63                 list.add(matrix[rowLowerBound][colLowerBound]);
64                 break;
65             }
66         }
67         return list;
68     }
69 }

 

以上是关于leetCode 54. Spiral Matrix的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode]Spiral Matrix 54

[LeetCode]54. Spiral Matrix

Leetcode54 Spiral Matrix

[leetcode][54] Spiral Matrix

LeetCode54 Spiral Matrix

LeetCode-54-Spiral Matrix