Skip to content

Commit 988e2f3

Browse files
committed
54. Spiral Matrix
1 parent 2c52d47 commit 988e2f3

File tree

7 files changed

+317
-2
lines changed

7 files changed

+317
-2
lines changed

markdown/54. Spiral Matrix.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
### [54\. Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a matrix of _m_ x _n_ elements (_m_ rows, _n_ columns), return all elements of the matrix in spiral order.
7+
8+
**Example 1:**
9+
10+
```
11+
Input:
12+
[
13+
[ 1, 2, 3 ],
14+
[ 4, 5, 6 ],
15+
[ 7, 8, 9 ]
16+
]
17+
Output: [1,2,3,6,9,8,7,4,5]
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input:
24+
[
25+
[1, 2, 3, 4],
26+
[5, 6, 7, 8],
27+
[9,10,11,12]
28+
]
29+
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
30+
```
31+
32+
33+
#### Solution
34+
35+
Language: **Java** My Answer
36+
37+
```java
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
41+
class Solution {
42+
public List<Integer> spiralOrder(int[][] matrix) {
43+
List<Integer> trace = new ArrayList<>();
44+
if (matrix == null || matrix.length == 0) {
45+
return trace;
46+
}
47+
int height = matrix.length;
48+
int wide = matrix[0].length;
49+
boolean[][] marked = new boolean[matrix.length][matrix[0].length];
50+
int moveDirection = 0;// 走一个 0-右 1-下 2-左 3-上 的轨迹
51+
int i = 0;
52+
int j = 0;
53+
do {
54+
trace.add(matrix[i][j]);
55+
marked[i][j] = true;
56+
switch (moveDirection) { // 确定下一步的路径绘制方向
57+
case 0:
58+
if (j + 1 >= wide || marked[i][j + 1]) {
59+
moveDirection += 1;
60+
i++;
61+
} else {
62+
j++;
63+
}
64+
break;
65+
case 1:
66+
if (i + 1 >= height || marked[i + 1][j]) {
67+
moveDirection += 1;
68+
j--;
69+
} else {
70+
i++;
71+
}
72+
break;
73+
case 2:
74+
if (j - 1 < 0 || marked[i][j - 1]) {
75+
moveDirection += 1;
76+
i--;
77+
} else {
78+
j--;
79+
}
80+
break;
81+
case 3:
82+
if (i - 1 < 0 || marked[i - 1][j]) {
83+
moveDirection = 0;
84+
j++;
85+
} else {
86+
i--;
87+
}
88+
break;
89+
default:
90+
break;
91+
}
92+
93+
} while (avaliable(marked, i, j + 1)
94+
|| avaliable(marked, i + 1, j)
95+
|| avaliable(marked, i, j - 1)
96+
|| avaliable(marked, i - 1, j));
97+
if (i >= 0 && i < height && j >= 0 && j < wide) { // 如果最后一个存在,加入。
98+
trace.add(matrix[i][j]);
99+
}
100+
return trace;
101+
}
102+
103+
private boolean avaliable(boolean[][] marked, int i, int j) {
104+
if (i >= marked.length || j >= marked[0].length || i < 0 || j < 0) {
105+
return false;
106+
}
107+
return !marked[i][j];
108+
}
109+
}
110+
```
111+
![](http://ww2.sinaimg.cn/large/006tNc79ly1g4wcnos1erj31410u01kx.jpg)

src/main/java/leetcode/_51_/Main.java renamed to src/main/java/leetcode/_53_/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package leetcode._51_;
1+
package leetcode._53_;
22

33
/**
44
* Created by zhangbo54 on 2019-03-04.

src/main/java/leetcode/_51_/Solution.java renamed to src/main/java/leetcode/_53_/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package leetcode._51_;
1+
package leetcode._53_;
22

33
class Solution {
44
public int maxSubArray(int[] nums) {

src/main/java/leetcode/_54_/Main.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode._54_;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
int[][] matrix = {
12+
{1, 2, 3, 4},
13+
{5, 6, 7, 8},
14+
{9, 10, 11, 12}};
15+
List<Integer> integers = solution.spiralOrder(matrix);
16+
System.out.println(integers);
17+
}
18+
}
19+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package leetcode._54_;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class Solution {
7+
public List<Integer> spiralOrder(int[][] matrix) {
8+
List<Integer> trace = new ArrayList<>();
9+
if (matrix == null || matrix.length == 0) {
10+
return trace;
11+
}
12+
int height = matrix.length;
13+
int wide = matrix[0].length;
14+
boolean[][] marked = new boolean[matrix.length][matrix[0].length];
15+
int moveDirection = 0;// 走一个 0-右 1-下 2-左 3-上 的轨迹
16+
int i = 0;
17+
int j = 0;
18+
do {
19+
trace.add(matrix[i][j]);
20+
marked[i][j] = true;
21+
switch (moveDirection) { // 确定下一步的路径绘制方向
22+
case 0:
23+
if (j + 1 >= wide || marked[i][j + 1]) {
24+
moveDirection += 1;
25+
i++;
26+
} else {
27+
j++;
28+
}
29+
break;
30+
case 1:
31+
if (i + 1 >= height || marked[i + 1][j]) {
32+
moveDirection += 1;
33+
j--;
34+
} else {
35+
i++;
36+
}
37+
break;
38+
case 2:
39+
if (j - 1 < 0 || marked[i][j - 1]) {
40+
moveDirection += 1;
41+
i--;
42+
} else {
43+
j--;
44+
}
45+
break;
46+
case 3:
47+
if (i - 1 < 0 || marked[i - 1][j]) {
48+
moveDirection = 0;
49+
j++;
50+
} else {
51+
i--;
52+
}
53+
break;
54+
default:
55+
break;
56+
}
57+
58+
} while (avaliable(marked, i, j + 1)
59+
|| avaliable(marked, i + 1, j)
60+
|| avaliable(marked, i, j - 1)
61+
|| avaliable(marked, i - 1, j));
62+
if (i >= 0 && i < height && j >= 0 && j < wide) { // 如果最后一个存在,加入。
63+
trace.add(matrix[i][j]);
64+
}
65+
return trace;
66+
}
67+
68+
private boolean avaliable(boolean[][] marked, int i, int j) {
69+
if (i >= marked.length || j >= marked[0].length || i < 0 || j < 0) {
70+
return false;
71+
}
72+
return !marked[i][j];
73+
}
74+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
### [54\. Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a matrix of _m_ x _n_ elements (_m_ rows, _n_ columns), return all elements of the matrix in spiral order.
7+
8+
**Example 1:**
9+
10+
```
11+
Input:
12+
[
13+
[ 1, 2, 3 ],
14+
[ 4, 5, 6 ],
15+
[ 7, 8, 9 ]
16+
]
17+
Output: [1,2,3,6,9,8,7,4,5]
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input:
24+
[
25+
[1, 2, 3, 4],
26+
[5, 6, 7, 8],
27+
[9,10,11,12]
28+
]
29+
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
30+
```
31+
32+
33+
#### Solution
34+
35+
Language: **Java** My Answer
36+
37+
```java
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
41+
class Solution {
42+
public List<Integer> spiralOrder(int[][] matrix) {
43+
List<Integer> trace = new ArrayList<>();
44+
if (matrix == null || matrix.length == 0) {
45+
return trace;
46+
}
47+
int height = matrix.length;
48+
int wide = matrix[0].length;
49+
boolean[][] marked = new boolean[matrix.length][matrix[0].length];
50+
int moveDirection = 0;// 走一个 0-右 1-下 2-左 3-上 的轨迹
51+
int i = 0;
52+
int j = 0;
53+
do {
54+
trace.add(matrix[i][j]);
55+
marked[i][j] = true;
56+
switch (moveDirection) { // 确定下一步的路径绘制方向
57+
case 0:
58+
if (j + 1 >= wide || marked[i][j + 1]) {
59+
moveDirection += 1;
60+
i++;
61+
} else {
62+
j++;
63+
}
64+
break;
65+
case 1:
66+
if (i + 1 >= height || marked[i + 1][j]) {
67+
moveDirection += 1;
68+
j--;
69+
} else {
70+
i++;
71+
}
72+
break;
73+
case 2:
74+
if (j - 1 < 0 || marked[i][j - 1]) {
75+
moveDirection += 1;
76+
i--;
77+
} else {
78+
j--;
79+
}
80+
break;
81+
case 3:
82+
if (i - 1 < 0 || marked[i - 1][j]) {
83+
moveDirection = 0;
84+
j++;
85+
} else {
86+
i--;
87+
}
88+
break;
89+
default:
90+
break;
91+
}
92+
93+
} while (avaliable(marked, i, j + 1)
94+
|| avaliable(marked, i + 1, j)
95+
|| avaliable(marked, i, j - 1)
96+
|| avaliable(marked, i - 1, j));
97+
if (i >= 0 && i < height && j >= 0 && j < wide) { // 如果最后一个存在,加入。
98+
trace.add(matrix[i][j]);
99+
}
100+
return trace;
101+
}
102+
103+
private boolean avaliable(boolean[][] marked, int i, int j) {
104+
if (i >= marked.length || j >= marked[0].length || i < 0 || j < 0) {
105+
return false;
106+
}
107+
return !marked[i][j];
108+
}
109+
}
110+
```
111+
![](http://ww2.sinaimg.cn/large/006tNc79ly1g4wcnos1erj31410u01kx.jpg)

0 commit comments

Comments
 (0)