Skip to content

Commit 9cb1b97

Browse files
committed
Add 54. Spiral Matrix solution
1 parent 51b85d6 commit 9cb1b97

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

leetcode/146.lru-cache.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
2. We will put the sentinel node at head and tail (always be there and we don't move the two ponters) to prevent extra null/empty check.
66

77
```js
8-
sentinel -> sentinel
8+
sentinel <-> sentinel
99
[head] [tail]
1010

1111
// Insert 1 at first
12-
sentinel -> 1 -> sentinel
12+
sentinel <-> 1 <-> sentinel
1313
[head] [tail]
1414

1515
// Insert 2 at last
16-
sentinel -> 1 -> 2 -> sentinel
16+
sentinel <-> 1 <-> 2 <-> sentinel
1717
[head] [tail]
1818

1919
// Remove 2, 1 at last
20-
sentinel -> sentinel
20+
sentinel <-> sentinel
2121
[head] [tail]
2222
```
2323

leetcode/54.spiral-matrix.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## [54. Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)
2+
3+
```kotlin
4+
fun spiralOrder(matrix: Array<IntArray>): List<Int> {
5+
val elements = mutableListOf<Int>()
6+
val m = matrix.size
7+
val n = matrix[0].size
8+
val directions = arrayOf(
9+
intArrayOf(0, 1), // Right
10+
intArrayOf(1, 0), // Down
11+
intArrayOf(0, -1), // Left
12+
intArrayOf(-1, 0) // UP
13+
)
14+
15+
val visited = Array(m) { _ -> BooleanArray(n) }
16+
var direction = 0
17+
var row = 0
18+
var col = 0
19+
while (elements.size < m * n) {
20+
elements.add(matrix[row][col])
21+
visited[row][col] = true
22+
23+
val newRow = row + directions[direction][0]
24+
val newCol = col + directions[direction][1]
25+
26+
if (newRow < 0 || newRow >= m || newCol < 0 || newCol >= n || visited[newRow][newCol]) {
27+
direction = (direction + 1) % directions.size
28+
}
29+
30+
row += directions[direction][0]
31+
col += directions[direction][1]
32+
}
33+
34+
return elements
35+
}
36+
```

topics/problems-solutions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
|[217. Contains Duplicate](../leetcode/217.contains-duplicate.md)|Easy|
1111
|[350. Intersection of Two Arrays II](../leetcode/350.intersection-of-two-arrays-ii.md)|Easy|
1212
|[48. Rotate Image](../leetcode/48.rotate-image.md)|Medium|
13+
|[54. Spiral Matrix](../leetcode/54.spiral-matrix.md)|Medium|
1314
|[59. Spiral Matrix II](../leetcode/59.spiral-matrix-ii.md)|Medium|
1415
|[189. Rotate Array](../leetcode/189.rotate-array.md)|Medium|
1516
|[136. Single Number](../leetcode/136.single-number.md)|Easy|
@@ -26,7 +27,6 @@
2627
|[76. Minimum Window Substring](../leetcode/76.minimum-window-substring.md)|Hard|
2728
|[239. Sliding Window Maximum](../leetcode/239.sliding-window-maximium.md)|Hard|
2829

29-
> * https://leetcode.com/problems/spiral-matrix/ 7k m
3030
> * https://leetcode.com/problems/find-all-duplicates-in-an-array/ 6k m
3131
> * https://leetcode.com/problems/merge-intervals/ 14k m
3232
> * https://leetcode.com/problems/container-with-most-water/ 17k m

0 commit comments

Comments
 (0)