diff --git a/solution/0000-0099/0001.Two Sum/README.md b/solution/0000-0099/0001.Two Sum/README.md index 48c03a89e9c22..74e45489db964 100644 --- a/solution/0000-0099/0001.Two Sum/README.md +++ b/solution/0000-0099/0001.Two Sum/README.md @@ -302,6 +302,25 @@ def two_sum(nums, target) end ``` +#### Kotlin + +```kotlin +class Solution { + fun twoSum(nums: IntArray, target: Int): IntArray { + val m = mutableMapOf() + nums.forEachIndexed { i, x -> + val y = target - x + val j = m.get(y) + if (j != null) { + return intArrayOf(j, i) + } + m[x] = i + } + return intArrayOf() + } +} +``` + #### Nim ```nim @@ -318,21 +337,19 @@ proc twoSum(nums: seq[int], target: int): seq[int] = return @[] ``` -#### Kotlin +#### Cangjie -```kotlin +```cj class Solution { - fun twoSum(nums: IntArray, target: Int): IntArray { - val m = mutableMapOf() - nums.forEachIndexed { i, x -> - val y = target - x - val j = m.get(y) - if (j != null) { - return intArrayOf(j, i) + func twoSum(nums: Array, target: Int64): Array { + let d = HashMap() + for (i in 0..nums.size) { + if (d.contains(target - nums[i])) { + return [d[target - nums[i]], i] } - m[x] = i + d[nums[i]] = i } - return intArrayOf() + [] } } ``` diff --git a/solution/0000-0099/0001.Two Sum/README_EN.md b/solution/0000-0099/0001.Two Sum/README_EN.md index 38066a682c1f8..2e2b638512a97 100644 --- a/solution/0000-0099/0001.Two Sum/README_EN.md +++ b/solution/0000-0099/0001.Two Sum/README_EN.md @@ -299,6 +299,25 @@ def two_sum(nums, target) end ``` +#### Kotlin + +```kotlin +class Solution { + fun twoSum(nums: IntArray, target: Int): IntArray { + val m = mutableMapOf() + nums.forEachIndexed { i, x -> + val y = target - x + val j = m.get(y) + if (j != null) { + return intArrayOf(j, i) + } + m[x] = i + } + return intArrayOf() + } +} +``` + #### Nim ```nim @@ -315,21 +334,19 @@ proc twoSum(nums: seq[int], target: int): seq[int] = return @[] ``` -#### Kotlin +#### Cangjie -```kotlin +```cj class Solution { - fun twoSum(nums: IntArray, target: Int): IntArray { - val m = mutableMapOf() - nums.forEachIndexed { i, x -> - val y = target - x - val j = m.get(y) - if (j != null) { - return intArrayOf(j, i) + func twoSum(nums: Array, target: Int64): Array { + let d = HashMap() + for (i in 0..nums.size) { + if (d.contains(target - nums[i])) { + return [d[target - nums[i]], i] } - m[x] = i + d[nums[i]] = i } - return intArrayOf() + [] } } ``` diff --git a/solution/0000-0099/0001.Two Sum/Solution.cj b/solution/0000-0099/0001.Two Sum/Solution.cj new file mode 100644 index 0000000000000..2ca66e33ab56d --- /dev/null +++ b/solution/0000-0099/0001.Two Sum/Solution.cj @@ -0,0 +1,12 @@ +class Solution { + func twoSum(nums: Array, target: Int64): Array { + let d = HashMap() + for (i in 0..nums.size) { + if (d.contains(target - nums[i])) { + return [d[target - nums[i]], i] + } + d[nums[i]] = i + } + [] + } +}