본문 바로가기
Algorithm/LeetCode

[LeetCode] Two Sum (Kotlin)

by dvid 2022. 12. 2.
class Solution {
    fun twoSum(nums: IntArray, target: Int): IntArray {
        val map = mutableMapOf<Int, Int>()
        for ((i, v) in nums.withIndex()) {
            if (map.containsKey(target - v)) {
                return intArrayOf(map[target - v]!!, i)
            }
            map[v] = i
        }
        return intArrayOf()
    }
}

댓글