Skip to content

Commit 89d404c

Browse files
48: Rotate Image
1 parent ca8462e commit 89d404c

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ My solutions to LeetCode problems in Kotlin.
1111
| [8](https://leetcode.com/problems/string-to-integer-atoi/) | [String to Integer (atoi)](src/main/kotlin/com/schmoczer/leetcode/_0008/StringToInteger.kt) | Medium |
1212
| [20](https://leetcode.com/problems/valid-parentheses/) | [Valid Parentheses](src/main/kotlin/com/schmoczer/leetcode/_0020/ValidParentheses.kt) | Easy |
1313
| [42](https://leetcode.com/problems/trapping-rain-water/) | [Trapping Rain Water](src/main/kotlin/com/schmoczer/leetcode/_0042/TrappingRainWater.kt) | Hard |
14+
| [48](https://leetcode.com/problems/rotate-image/) | [Rotate Image](src/main/kotlin/com/schmoczer/leetcode/_0048/RotateImage.kt) | Medium |
1415
| [49](https://leetcode.com/problems/group-anagrams/) | [Group Anagrams](src/main/kotlin/com/schmoczer/leetcode/_0049/GroupAnagrams.kt) | Medium |
1516
| [73](https://leetcode.com/problems/set-matrix-zeroes/) | [Set Matrix Zeroes](src/main/kotlin/com/schmoczer/leetcode/_0073/SetMatrixZeroes.kt) | Medium |
1617
| [125](https://leetcode.com/problems/valid-palindrome/) | [Valid Palindrome](src/main/kotlin/com/schmoczer/leetcode/_0125/ValidPalindrome.kt) | Easy |
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Rotate Image
2+
3+
You are given an `n x n` 2D `matrix` representing an image, rotate the image by 90 degrees (clockwise).
4+
5+
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate
6+
another 2D matrix and do the rotation.
7+
8+
Example 1:
9+
10+
> Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
11+
>
12+
> Output: [[7,4,1],[8,5,2],[9,6,3]]
13+
14+
Example 2:
15+
16+
> Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
17+
>
18+
> Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
19+
20+
Constraints:
21+
22+
- `n == matrix.length == matrix[i].length`
23+
- `1 <= n <= 20`
24+
- `-1000 <= matrix[i][j] <= 1000`
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.schmoczer.leetcode._0048
2+
3+
class RotateImage {
4+
// Runtime 0ms Beats 100.00%
5+
fun rotate(matrix: Array<IntArray>) {
6+
var i = 0
7+
while (i < matrix.size - 1 - i) {
8+
val width = matrix.size - i - 1
9+
for (j in i until width) {
10+
val up = matrix[i][j]
11+
val right = matrix[j][width]
12+
val down = matrix[width][matrix.size - 1 - j]
13+
val left = matrix[matrix.size - 1 - j][i]
14+
matrix[j][width] = up
15+
matrix[width][matrix.size - 1 - j] = right
16+
matrix[matrix.size - 1 - j][i] = down
17+
matrix[i][j] = left
18+
}
19+
i++
20+
}
21+
}
22+
}
23+
24+
// width = 4 - 0 = 4
25+
// i = 0, j = 0, right should be: (0, 3)
26+
// first: j = 0, second: width - 1 = 3?
27+
// down should be: (3, 3)
28+
// row: width - 1 = 3, column: width - 1 - j = 3
29+
// left should be: (3, 0)
30+
//
31+
// i = 0, j = 1, right should be: (1, 3)
32+
// first: j = 1, second: widht - 1 = 3
33+
// down should be (3, 2)
34+
// row: width - 1, column: width - 1 - j = 2
35+
// left should be (2, 0)
36+
//
37+
// i = 0, j = 2, right should (2, 3)
38+
//
39+
// width = 4 - 1 = 3
40+
// i = 1, j = 1, right should be (1, 2)
41+
// row: j = 1, column: width - 1 = 2
42+
// down should be (2, 2)
43+
// row: width - 1 = 2, column: width - 1 - j =
44+
// left should be (2, 1)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.schmoczer.leetcode._0048
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertContentEquals
6+
7+
class RotateImageTest {
8+
private lateinit var sut: RotateImage
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = RotateImage()
13+
}
14+
15+
@Test
16+
fun `rotate 3x3 matrix`() {
17+
val input = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6), intArrayOf(7, 8, 9))
18+
val expected = arrayOf(intArrayOf(7, 4, 1), intArrayOf(8, 5, 2), intArrayOf(9, 6, 3))
19+
20+
sut.rotate(input)
21+
22+
for (i in 0 until expected.size) {
23+
assertContentEquals(expected[i], input[i])
24+
}
25+
}
26+
27+
@Test
28+
fun `rotate 4x4 matrix`() {
29+
val input = arrayOf(intArrayOf(5, 1, 9, 11), intArrayOf(2, 4, 8, 10), intArrayOf(13, 3, 6, 7), intArrayOf(15, 14, 12, 16))
30+
val expected = arrayOf(intArrayOf(15, 13, 2, 5), intArrayOf(14, 3, 4, 1), intArrayOf(12, 6, 8, 9), intArrayOf(16, 7, 10, 11))
31+
32+
sut.rotate(input)
33+
34+
for (i in 0 until expected.size) {
35+
assertContentEquals(expected[i], input[i])
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)