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 ca8462e

Browse files
73: Set Matrix Zeroes
1 parent 81b8183 commit ca8462e

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ My solutions to LeetCode problems in Kotlin.
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 |
1414
| [49](https://leetcode.com/problems/group-anagrams/) | [Group Anagrams](src/main/kotlin/com/schmoczer/leetcode/_0049/GroupAnagrams.kt) | Medium |
15+
| [73](https://leetcode.com/problems/set-matrix-zeroes/) | [Set Matrix Zeroes](src/main/kotlin/com/schmoczer/leetcode/_0073/SetMatrixZeroes.kt) | Medium |
1516
| [125](https://leetcode.com/problems/valid-palindrome/) | [Valid Palindrome](src/main/kotlin/com/schmoczer/leetcode/_0125/ValidPalindrome.kt) | Easy |
1617
| [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 |
1718
| [186](https://leetcode.com/problems/reverse-words-in-a-string-ii/) | [Reverse Words in a String II](src/main/kotlin/com/schmoczer/leetcode/_0186/ReverseWordsInStringInPlace.kt) | Medium |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Set Matrix Zeroes
2+
3+
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s.
4+
5+
You must do it in place.
6+
7+
Example 1:
8+
9+
> Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
10+
>
11+
> Output: [[1,0,1],[0,0,0],[1,0,1]]
12+
13+
Example 2:
14+
15+
> Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
16+
>
17+
> Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
18+
19+
20+
Constraints:
21+
22+
- `m == matrix.length`
23+
- `n == matrix[0].length`
24+
- `1 <= m, n <= 200`
25+
- `-2^31 <= matrix[i][j] <= 2^31 - 1`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.schmoczer.leetcode._0073
2+
3+
class SetMatrixZeroes {
4+
fun setZeroes(matrix: Array<IntArray>) {
5+
val foundZeroes = mutableListOf<Pair<Int, Int>>()
6+
for (i in 0 until matrix.size) {
7+
for (j in 0 until matrix.first().size) {
8+
if (matrix[i][j] == 0) {
9+
foundZeroes.add(Pair(i, j))
10+
}
11+
}
12+
}
13+
for (foundZero in foundZeroes) {
14+
for (i in 0 until matrix.first().size) {
15+
matrix[foundZero.first][i] = 0
16+
}
17+
for (i in 0 until matrix.size) {
18+
matrix[i][foundZero.second] = 0
19+
}
20+
}
21+
}
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.schmoczer.leetcode._0073
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertContentEquals
6+
7+
class SetMatrixZeroesTest {
8+
private lateinit var sut: SetMatrixZeroes
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = SetMatrixZeroes()
13+
}
14+
15+
@Test
16+
fun `set zeroes on 3x3 matrix`() {
17+
val input = arrayOf(intArrayOf(1, 1, 1), intArrayOf(1, 0, 1), intArrayOf(1, 1, 1))
18+
val expected = arrayOf(intArrayOf(1, 0, 1), intArrayOf(0, 0, 0), intArrayOf(1, 0, 1))
19+
20+
sut.setZeroes(input)
21+
22+
for (i in 0 until expected.size) {
23+
assertContentEquals(expected[i], input[i])
24+
}
25+
}
26+
27+
@Test
28+
fun `set zeroes on 3x4 matrix`() {
29+
val input = arrayOf(intArrayOf(0, 1, 2, 0), intArrayOf(3, 4, 5, 2), intArrayOf(1, 3, 1, 5))
30+
val expected = arrayOf(intArrayOf(0, 0, 0, 0), intArrayOf(0, 4, 5, 0), intArrayOf(0, 3, 1, 0))
31+
32+
sut.setZeroes(input)
33+
34+
for (i in 0 until expected.size) {
35+
assertContentEquals(expected[i], input[i])
36+
}
37+
}
38+
}

0 commit comments

Comments
(0)

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