-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathReshapeTheMatrix.kt
66 lines (60 loc) · 1.82 KB
/
ReshapeTheMatrix.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* You are given an `m x n` matrix `mat` and two integers `r` and `c`
* representing the number of rows and the number of columns of the wanted reshaped matrix.
* The reshaped matrix should be filled with all the elements of the original matrix
* in the same row-traversing order as they were.
*
* If the reshape operation with given parameters is possible and legal, output the new reshaped matrix;
* Otherwise, output the original matrix.
*
* [Source](https://leetcode.com/problems/reshape-the-matrix/)
*/
@UseCommentAsDocumentation
private fun matrixReshape(mat: Array<IntArray>, r: Int, c: Int): Array<IntArray> {
val col = mat[0].size
val row = mat.size
if (r * c != col * row) {
return mat
}
val result = Array<IntArray>(r) { IntArray(c) { -1 } }
var rowIndex = 0
var colIndex = 0
for (i in 0 until row) {
for (j in 0 until col) {
if (colIndex >= c) {
rowIndex++
colIndex = 0
}
result[rowIndex][colIndex] = mat[i][j]
colIndex++
}
}
return result
}
fun main() {
run() {
val result = matrixReshape(mat = arrayOf(intArrayOf(1, 2), intArrayOf(3, 4)), r = 1, c = 4)
val expected = arrayOf(intArrayOf(1, 2, 3, 4))
result.forEachIndexed { index, ints ->
ints shouldBe expected[index]
}
}
run {
val result = matrixReshape(
mat = arrayOf(
intArrayOf(1, 2),
intArrayOf(3, 4)
), r = 2, c = 4
)
val expected = arrayOf(
intArrayOf(1, 2),
intArrayOf(3, 4)
)
result.forEachIndexed { index, ints ->
ints shouldBe expected[index]
}
}
}