diff --git a/kotlin/Two-Sum.kt b/kotlin/Two-Sum.kt new file mode 100644 index 000000000..62c1c604c --- /dev/null +++ b/kotlin/Two-Sum.kt @@ -0,0 +1,21 @@ +package kotlin + +class Solution { + fun twoSum(nums: IntArray, target: Int): IntArray { + val prevMap: HashMap = HashMap() + for (i in nums.indices) { + val num = nums[i] + val diff = target - num + if (prevMap.containsKey(diff)) { + return intArrayOf(prevMap[diff]!!, i) + } + prevMap[num] = i + } + return intArrayOf() + } +} + +fun main() { + val result = Solution().twoSum(intArrayOf(2, 7, 11, 15), 9) + println(result.contentToString()) +} \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy