Skip to content

Commit 7ffd30e

Browse files
committed
feat: add kotlin solution to lc problem: No.0304
1 parent 0aa3e9d commit 7ffd30e

File tree

3 files changed

+147
-2
lines changed

3 files changed

+147
-2
lines changed

solution/0300-0399/0304.Range Sum Query 2D - Immutable/README.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ tags:
3939
<p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/images/1626332422-wUpUHT-image.png" style="width: 200px;" /></p>
4040

4141
<pre>
42-
<strong>输入:</strong>
42+
<strong>输入:</strong>
4343
["NumMatrix","sumRegion","sumRegion","sumRegion"]
4444
[[[[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]]
45-
<strong>输出:</strong>
45+
<strong>输出:</strong>
4646
[null, 8, 11, 12]
4747

4848
<strong>解释:</strong>
@@ -347,6 +347,56 @@ NumMatrix.prototype.sumRegion = function (row1, col1, row2, col2) {
347347
*/
348348
```
349349

350+
#### Kotlin
351+
352+
```kotlin
353+
class NumMatrix(matrix: Array<IntArray>) {
354+
private val n: Int
355+
private val m: Int
356+
private val matrix: Array<IntArray>
357+
private val prefix_sums_matrix: Array<IntArray>
358+
private var initialized: Boolean
359+
360+
init {
361+
this.n = matrix.size
362+
this.m = matrix[0].size
363+
this.matrix = matrix
364+
this.prefix_sums_matrix = Array(n + 1) { IntArray(m + 1) }
365+
this.initialized = false
366+
}
367+
368+
fun sumRegion(row1: Int, col1: Int, row2: Int, col2: Int): Int {
369+
this.init()
370+
return this.prefix_sums_matrix[row2 + 1][col2 + 1] -
371+
this.prefix_sums_matrix[row2 + 1][col1] -
372+
this.prefix_sums_matrix[row1][col2 + 1] +
373+
this.prefix_sums_matrix[row1][col1]
374+
}
375+
376+
private fun init(): Boolean {
377+
if (!this.initialized) {
378+
for (i in 0..<this.n) {
379+
for (j in 0..<this.m) {
380+
this.prefix_sums_matrix[i + 1][j + 1] =
381+
this.prefix_sums_matrix[i + 1][j] +
382+
this.prefix_sums_matrix[i][j + 1] -
383+
this.prefix_sums_matrix[i][j] +
384+
this.matrix[i][j]
385+
}
386+
}
387+
this.initialized = true
388+
return true
389+
}
390+
return false
391+
}
392+
}
393+
394+
/**
395+
* Your NumMatrix object will be instantiated and called as such: var obj = NumMatrix(matrix) var
396+
* param_1 = obj.sumRegion(row1,col1,row2,col2)
397+
*/
398+
```
399+
350400
<!-- tabs:end -->
351401

352402
<!-- solution:end -->

solution/0300-0399/0304.Range Sum Query 2D - Immutable/README_EN.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,56 @@ NumMatrix.prototype.sumRegion = function (row1, col1, row2, col2) {
346346
*/
347347
```
348348

349+
#### Kotlin
350+
351+
```kotlin
352+
class NumMatrix(matrix: Array<IntArray>) {
353+
private val n: Int
354+
private val m: Int
355+
private val matrix: Array<IntArray>
356+
private val prefix_sums_matrix: Array<IntArray>
357+
private var initialized: Boolean
358+
359+
init {
360+
this.n = matrix.size
361+
this.m = matrix[0].size
362+
this.matrix = matrix
363+
this.prefix_sums_matrix = Array(n + 1) { IntArray(m + 1) }
364+
this.initialized = false
365+
}
366+
367+
fun sumRegion(row1: Int, col1: Int, row2: Int, col2: Int): Int {
368+
this.init()
369+
return this.prefix_sums_matrix[row2 + 1][col2 + 1] -
370+
this.prefix_sums_matrix[row2 + 1][col1] -
371+
this.prefix_sums_matrix[row1][col2 + 1] +
372+
this.prefix_sums_matrix[row1][col1]
373+
}
374+
375+
private fun init(): Boolean {
376+
if (!this.initialized) {
377+
for (i in 0..<this.n) {
378+
for (j in 0..<this.m) {
379+
this.prefix_sums_matrix[i + 1][j + 1] =
380+
this.prefix_sums_matrix[i + 1][j] +
381+
this.prefix_sums_matrix[i][j + 1] -
382+
this.prefix_sums_matrix[i][j] +
383+
this.matrix[i][j]
384+
}
385+
}
386+
this.initialized = true
387+
return true
388+
}
389+
return false
390+
}
391+
}
392+
393+
/**
394+
* Your NumMatrix object will be instantiated and called as such: var obj = NumMatrix(matrix) var
395+
* param_1 = obj.sumRegion(row1,col1,row2,col2)
396+
*/
397+
```
398+
349399
<!-- tabs:end -->
350400

351401
<!-- solution:end -->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class NumMatrix(matrix: Array<IntArray>) {
2+
private val n: Int
3+
private val m: Int
4+
private val matrix: Array<IntArray>
5+
private val prefix_sums_matrix: Array<IntArray>
6+
private var initialized: Boolean
7+
8+
init {
9+
this.n = matrix.size
10+
this.m = matrix[0].size
11+
this.matrix = matrix
12+
this.prefix_sums_matrix = Array(n + 1) { IntArray(m + 1) }
13+
this.initialized = false
14+
}
15+
16+
fun sumRegion(row1: Int, col1: Int, row2: Int, col2: Int): Int {
17+
this.init()
18+
return this.prefix_sums_matrix[row2 + 1][col2 + 1] -
19+
this.prefix_sums_matrix[row2 + 1][col1] -
20+
this.prefix_sums_matrix[row1][col2 + 1] +
21+
this.prefix_sums_matrix[row1][col1]
22+
}
23+
24+
private fun init(): Boolean {
25+
if (!this.initialized) {
26+
for (i in 0..<this.n) {
27+
for (j in 0..<this.m) {
28+
this.prefix_sums_matrix[i + 1][j + 1] =
29+
this.prefix_sums_matrix[i + 1][j] +
30+
this.prefix_sums_matrix[i][j + 1] -
31+
this.prefix_sums_matrix[i][j] +
32+
this.matrix[i][j]
33+
}
34+
}
35+
this.initialized = true
36+
return true
37+
}
38+
return false
39+
}
40+
}
41+
42+
/**
43+
* Your NumMatrix object will be instantiated and called as such: var obj = NumMatrix(matrix) var
44+
* param_1 = obj.sumRegion(row1,col1,row2,col2)
45+
*/

0 commit comments

Comments
 (0)