class Solution {
fun twoSum(nums: IntArray, target: Int): IntArray {
val prevMap = hashMapOf<Int, Int>()
for ((i, num) in nums.withIndex()) {
val diff = target - num
if (prevMap.containsKey(diff)) {
return intArrayOf(prevMap[diff]!!, i)
}
prevMap[num] = i
}
return intArrayOf()
}
}
fun main() {
val nums = intArrayOf(1, 2, 3, 4, 5)
for (i in 1..11) {
var result = Solution().twoSum(nums, i)
println(result.contentToString())
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: