File tree Expand file tree Collapse file tree 3 files changed +41
-5
lines changed Expand file tree Collapse file tree 3 files changed +41
-5
lines changed Original file line number Diff line number Diff line change 5
5
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.
6
6
7
7
``` js
8
- sentinel - > sentinel
8
+ sentinel < - > sentinel
9
9
[head] [tail]
10
10
11
11
// Insert 1 at first
12
- sentinel - > 1 - > sentinel
12
+ sentinel < - > 1 < - > sentinel
13
13
[head] [tail]
14
14
15
15
// Insert 2 at last
16
- sentinel - > 1 - > 2 - > sentinel
16
+ sentinel < - > 1 < - > 2 < - > sentinel
17
17
[head] [tail]
18
18
19
19
// Remove 2, 1 at last
20
- sentinel - > sentinel
20
+ sentinel < - > sentinel
21
21
[head] [tail]
22
22
```
23
23
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change 10
10
| [ 217. Contains Duplicate] ( ../leetcode/217.contains-duplicate.md ) | Easy|
11
11
| [ 350. Intersection of Two Arrays II] ( ../leetcode/350.intersection-of-two-arrays-ii.md ) | Easy|
12
12
| [ 48. Rotate Image] ( ../leetcode/48.rotate-image.md ) | Medium|
13
+ | [ 54. Spiral Matrix] ( ../leetcode/54.spiral-matrix.md ) | Medium|
13
14
| [ 59. Spiral Matrix II] ( ../leetcode/59.spiral-matrix-ii.md ) | Medium|
14
15
| [ 189. Rotate Array] ( ../leetcode/189.rotate-array.md ) | Medium|
15
16
| [ 136. Single Number] ( ../leetcode/136.single-number.md ) | Easy|
26
27
| [ 76. Minimum Window Substring] ( ../leetcode/76.minimum-window-substring.md ) | Hard|
27
28
| [ 239. Sliding Window Maximum] ( ../leetcode/239.sliding-window-maximium.md ) | Hard|
28
29
29
- > * https://leetcode.com/problems/spiral-matrix/ 7k m
30
30
> * https://leetcode.com/problems/find-all-duplicates-in-an-array/ 6k m
31
31
> * https://leetcode.com/problems/merge-intervals/ 14k m
32
32
> * https://leetcode.com/problems/container-with-most-water/ 17k m
You can’t perform that action at this time.
0 commit comments