|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
6 |
| -/** |
7 |
| - * 54. Spiral Matrix |
8 |
| - * |
9 |
| - * Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. |
10 |
| -
|
11 |
| - For example, |
12 |
| - Given the following matrix: |
13 |
| -
|
14 |
| - [ |
15 |
| - [ 1, 2, 3 ], |
16 |
| - [ 4, 5, 6 ], |
17 |
| - [ 7, 8, 9 ] |
18 |
| - ] |
19 |
| -
|
20 |
| - You should return [1,2,3,6,9,8,7,4,5]. |
21 |
| - */ |
22 | 6 | public class _54 {
|
23 | 7 |
|
24 |
| - public static class Solution1 { |
25 |
| - /**credit: https://leetcode.com/problems/spiral-matrix/discuss/20599/Super-Simple-and-Easy-to-Understand-Solution/185257*/ |
26 |
| - public List<Integer> spiralOrder(int[][] matrix) { |
27 |
| - if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { |
28 |
| - return new ArrayList<>(); |
29 |
| - } |
30 |
| - int m = matrix.length; |
31 |
| - int n = matrix[0].length; |
32 |
| - List<Integer> result = new ArrayList(); |
33 |
| - int left = 0; |
34 |
| - int right = n - 1; |
35 |
| - int top = 0; |
36 |
| - int bottom = m - 1; |
37 |
| - while (result.size() < m * n) { |
38 |
| - for (int j = left; j <= right && result.size() < m * n; j++) { |
39 |
| - result.add(matrix[top][j]); |
40 |
| - } |
41 |
| - top++; |
42 |
| - for (int i = top; i <= bottom && result.size() < m * n; i++) { |
43 |
| - result.add(matrix[i][right]); |
44 |
| - } |
45 |
| - right--; |
46 |
| - for (int j = right; j >= left && result.size() < m * n; j--) { |
47 |
| - result.add(matrix[bottom][j]); |
48 |
| - } |
49 |
| - bottom--; |
50 |
| - for (int i = bottom; i >= top && result.size() < m * n; i--) { |
51 |
| - result.add(matrix[i][left]); |
| 8 | + public static class Solution1 { |
| 9 | + /** |
| 10 | + * credit: https://leetcode.com/problems/spiral-matrix/discuss/20599/Super-Simple-and-Easy-to-Understand-Solution/185257 |
| 11 | + */ |
| 12 | + public List<Integer> spiralOrder(int[][] matrix) { |
| 13 | + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { |
| 14 | + return new ArrayList<>(); |
| 15 | + } |
| 16 | + int m = matrix.length; |
| 17 | + int n = matrix[0].length; |
| 18 | + List<Integer> result = new ArrayList(); |
| 19 | + int left = 0; |
| 20 | + int right = n - 1; |
| 21 | + int top = 0; |
| 22 | + int bottom = m - 1; |
| 23 | + while (result.size() < m * n) { |
| 24 | + for (int j = left; j <= right && result.size() < m * n; j++) { |
| 25 | + result.add(matrix[top][j]); |
| 26 | + } |
| 27 | + top++; |
| 28 | + for (int i = top; i <= bottom && result.size() < m * n; i++) { |
| 29 | + result.add(matrix[i][right]); |
| 30 | + } |
| 31 | + right--; |
| 32 | + for (int j = right; j >= left && result.size() < m * n; j--) { |
| 33 | + result.add(matrix[bottom][j]); |
| 34 | + } |
| 35 | + bottom--; |
| 36 | + for (int i = bottom; i >= top && result.size() < m * n; i--) { |
| 37 | + result.add(matrix[i][left]); |
| 38 | + } |
| 39 | + left++; |
| 40 | + } |
| 41 | + return result; |
52 | 42 | }
|
53 |
| - left++; |
54 |
| - } |
55 |
| - return result; |
56 | 43 | }
|
57 |
| - } |
58 | 44 | }
|
0 commit comments