Skip to content

Commit 02c489f

Browse files
committed
feat: add kotlin solution to lc problem: No.0001
1 parent 9d87743 commit 02c489f

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
@@ -319,6 +319,25 @@ proc twoSum(nums: seq[int], target: int): seq[int] =
319319
return @[idx, tdx]
320320
```
321321

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

324343
<!-- 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
@@ -316,6 +316,25 @@ proc twoSum(nums: seq[int], target: int): seq[int] =
316316
return @[idx, tdx]
317317
```
318318

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

321340
<!-- 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)