File tree Expand file tree Collapse file tree 3 files changed +68
-22
lines changed
solution/0000-0099/0001.Two Sum Expand file tree Collapse file tree 3 files changed +68
-22
lines changed Original file line number Diff line number Diff line change @@ -302,6 +302,25 @@ def two_sum(nums, target)
302302end
303303```
304304
305+ #### Kotlin
306+
307+ ``` kotlin
308+ class Solution {
309+ fun twoSum (nums : IntArray , target : Int ): IntArray {
310+ val m = mutableMapOf<Int , Int >()
311+ nums.forEachIndexed { i, x ->
312+ val y = target - x
313+ val j = m.get(y)
314+ if (j != null ) {
315+ return intArrayOf(j, i)
316+ }
317+ m[x] = i
318+ }
319+ return intArrayOf()
320+ }
321+ }
322+ ```
323+
305324#### Nim
306325
307326``` nim
@@ -318,21 +337,19 @@ proc twoSum(nums: seq[int], target: int): seq[int] =
318337 return @[]
319338```
320339
321- #### Kotlin
340+ #### Cangjie
322341
323- ``` kotlin
342+ ``` cj
324343class 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)
344+ func twoSum(nums: Array<Int64>, target: Int64): Array<Int64> {
345+ let d = HashMap<Int64, Int64>()
346+ for (i in 0..nums.size) {
347+ if (d.contains(target - nums[i])) {
348+ return [d[target - nums[i]], i]
332349 }
333- m[x ] = i
350+ d[nums[i] ] = i
334351 }
335- return intArrayOf()
352+ []
336353 }
337354}
338355```
Original file line number Diff line number Diff line change @@ -299,6 +299,25 @@ def two_sum(nums, target)
299299end
300300```
301301
302+ #### Kotlin
303+
304+ ``` kotlin
305+ class Solution {
306+ fun twoSum (nums : IntArray , target : Int ): IntArray {
307+ val m = mutableMapOf<Int , Int >()
308+ nums.forEachIndexed { i, x ->
309+ val y = target - x
310+ val j = m.get(y)
311+ if (j != null ) {
312+ return intArrayOf(j, i)
313+ }
314+ m[x] = i
315+ }
316+ return intArrayOf()
317+ }
318+ }
319+ ```
320+
302321#### Nim
303322
304323``` nim
@@ -315,21 +334,19 @@ proc twoSum(nums: seq[int], target: int): seq[int] =
315334 return @[]
316335```
317336
318- #### Kotlin
337+ #### Cangjie
319338
320- ``` kotlin
339+ ``` cj
321340class 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)
341+ func twoSum(nums: Array<Int64>, target: Int64): Array<Int64> {
342+ let d = HashMap<Int64, Int64>()
343+ for (i in 0..nums.size) {
344+ if (d.contains(target - nums[i])) {
345+ return [d[target - nums[i]], i]
329346 }
330- m[x ] = i
347+ d[nums[i] ] = i
331348 }
332- return intArrayOf()
349+ []
333350 }
334351}
335352```
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func twoSum(nums: Array<Int64>, target: Int64): Array<Int64> {
3+ let d = HashMap<Int64, Int64>()
4+ for (i in 0..nums.size) {
5+ if (d.contains(target - nums[i])) {
6+ return [d[target - nums[i]], i]
7+ }
8+ d[nums[i]] = i
9+ }
10+ []
11+ }
12+ }
You can’t perform that action at this time.
0 commit comments