Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 261d804

Browse files
54: Spiral Matrix
1 parent 89d404c commit 261d804

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ My solutions to LeetCode problems in Kotlin.
1313
| [42](https://leetcode.com/problems/trapping-rain-water/) | [Trapping Rain Water](src/main/kotlin/com/schmoczer/leetcode/_0042/TrappingRainWater.kt) | Hard |
1414
| [48](https://leetcode.com/problems/rotate-image/) | [Rotate Image](src/main/kotlin/com/schmoczer/leetcode/_0048/RotateImage.kt) | Medium |
1515
| [49](https://leetcode.com/problems/group-anagrams/) | [Group Anagrams](src/main/kotlin/com/schmoczer/leetcode/_0049/GroupAnagrams.kt) | Medium |
16+
| [54](https://leetcode.com/problems/spiral-matrix/) | [Spiral Matrix](src/main/kotlin/com/schmoczer/leetcode/_0054/SpiralMatrix.kt) | Medium |
1617
| [73](https://leetcode.com/problems/set-matrix-zeroes/) | [Set Matrix Zeroes](src/main/kotlin/com/schmoczer/leetcode/_0073/SetMatrixZeroes.kt) | Medium |
1718
| [125](https://leetcode.com/problems/valid-palindrome/) | [Valid Palindrome](src/main/kotlin/com/schmoczer/leetcode/_0125/ValidPalindrome.kt) | Easy |
1819
| [151](https://leetcode.com/problems/reverse-words-in-a-string/) | [Reverse Words in a String](src/main/kotlin/com/schmoczer/leetcode/_0151/ReverseWordsInString.kt) | Medium |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Spiral Matrix
2+
3+
Given an `m x n` `matrix`, return all elements of the `matrix` in spiral order.
4+
5+
Example 1:
6+
7+
> Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
8+
>
9+
> Output: [1,2,3,6,9,8,7,4,5]
10+
11+
Example 2:
12+
13+
> Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
14+
>
15+
> Output: [1,2,3,4,8,12,11,10,9,5,6,7]
16+
17+
Constraints:
18+
19+
- `m == matrix.length`
20+
- `n == matrix[i].length`
21+
- `1 <= m, n <= 10`
22+
- `-100 <= matrix[i][j] <= 100`
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.schmoczer.leetcode._0054
2+
3+
class SpiralMatrix {
4+
fun spiralOrder(matrix: Array<IntArray>): List<Int> {
5+
var leftBoundary = -1
6+
var upperBoundary = 0
7+
var rightBoundary = matrix.first().size
8+
var lowerBoundary = matrix.size
9+
val result = mutableListOf<Int>()
10+
11+
fun moveLeft(row: Int, column: Int): Pair<Int, Int> {
12+
for (i in (leftBoundary + 1 until column + 1).reversed()) {
13+
result.add(matrix[row][i])
14+
}
15+
leftBoundary++
16+
return Pair(row, leftBoundary)
17+
}
18+
19+
fun moveUp(row: Int, column: Int): Pair<Int, Int> {
20+
for (i in (upperBoundary + 1 until row + 1).reversed()) {
21+
result.add(matrix[i][column])
22+
}
23+
upperBoundary++
24+
return Pair(upperBoundary, column)
25+
}
26+
27+
fun moveRight(row: Int, column: Int): Pair<Int, Int> {
28+
for (i in column until rightBoundary) {
29+
result.add(matrix[row][i])
30+
}
31+
rightBoundary--
32+
return Pair(row, rightBoundary)
33+
}
34+
35+
fun moveDown(row: Int, column: Int): Pair<Int, Int> {
36+
for (i in row until lowerBoundary) {
37+
result.add(matrix[i][column])
38+
}
39+
lowerBoundary--
40+
return Pair(lowerBoundary, column)
41+
}
42+
43+
var row = 0
44+
var column = 0
45+
while (true) {
46+
val afterRight = moveRight(row, column)
47+
if (leftBoundary >= rightBoundary || upperBoundary >= lowerBoundary) break
48+
val afterDown = moveDown(afterRight.first + 1, afterRight.second)
49+
if (leftBoundary >= rightBoundary || upperBoundary >= lowerBoundary) break
50+
val afterLeft = moveLeft(afterDown.first, afterDown.second - 1)
51+
if (leftBoundary >= rightBoundary || upperBoundary >= lowerBoundary) break
52+
val afterUp = moveUp(afterLeft.first - 1, afterLeft.second)
53+
if (leftBoundary >= rightBoundary || upperBoundary >= lowerBoundary) break
54+
row = afterUp.first
55+
column = afterUp.second + 1
56+
}
57+
return result
58+
}
59+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.schmoczer.leetcode._0054
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
7+
class SpiralMatrixTest {
8+
private lateinit var sut: SpiralMatrix
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = SpiralMatrix()
13+
}
14+
15+
@Test
16+
fun `spiral a 3x3 matrix`() {
17+
val input = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6), intArrayOf(7, 8, 9))
18+
val exptected = listOf(1, 2, 3, 6, 9, 8, 7, 4, 5)
19+
20+
assertEquals(exptected, sut.spiralOrder(input))
21+
}
22+
23+
@Test
24+
fun `spiral a 4x4 matrix`() {
25+
val input = arrayOf(intArrayOf(1, 2, 3, 4), intArrayOf(5, 6, 7, 8), intArrayOf(9, 10, 11, 12))
26+
val expected = listOf(1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7)
27+
28+
assertEquals(expected, sut.spiralOrder(input))
29+
}
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /