Skip to content

Commit c5caac7

Browse files
committed
feat: add kotlin solution to lc problem: No.0001
1 parent 3281d77 commit c5caac7

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

solution/0000-0099/0001.Two Sum/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,25 @@ proc twoSum(nums: seq[int], target: int): seq[int] =
318318
return @[]
319319
```
320320

321+
#### Kotlin
322+
323+
```kotlin
324+
class Solution {
325+
fun twoSum(nums: IntArray, target: Int): IntArray {
326+
val m = mutableMapOf<Int, Int>()
327+
nums.forEachIndexed { i, x ->
328+
val y = target - x
329+
val j = m.get(y)
330+
if (j != null) {
331+
return intArrayOf(j, i)
332+
}
333+
m[x] = i
334+
}
335+
return intArrayOf()
336+
}
337+
}
338+
```
339+
321340
<!-- tabs:end -->
322341

323342
<!-- solution:end -->

solution/0000-0099/0001.Two Sum/README_EN.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,25 @@ proc twoSum(nums: seq[int], target: int): seq[int] =
315315
return @[]
316316
```
317317

318+
#### Kotlin
319+
320+
```kotlin
321+
class Solution {
322+
fun twoSum(nums: IntArray, target: Int): IntArray {
323+
val m = mutableMapOf<Int, Int>()
324+
nums.forEachIndexed { i, x ->
325+
val y = target - x
326+
val j = m.get(y)
327+
if (j != null) {
328+
return intArrayOf(j, i)
329+
}
330+
m[x] = i
331+
}
332+
return intArrayOf()
333+
}
334+
}
335+
```
336+
318337
<!-- tabs:end -->
319338

320339
<!-- solution:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
fun twoSum(nums: IntArray, target: Int): IntArray {
3+
val m = mutableMapOf<Int, Int>()
4+
nums.forEachIndexed { i, x ->
5+
val y = target - x
6+
val j = m.get(y)
7+
if (j != null) {
8+
return intArrayOf(j, i)
9+
}
10+
m[x] = i
11+
}
12+
return intArrayOf()
13+
}
14+
}

0 commit comments

Comments
 (0)