KotlinLeaner
09/06/2022, 8:20 PMlet
is going inside the lambda block if value is null. Can someone guide me on this. ThanksKotlinLeaner
09/06/2022, 8:20 PMInput: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
KotlinLeaner
09/06/2022, 8:21 PMfun main() {
// val nums = intArrayOf(2, 7, 11, 15)
// val target = 9
val nums = intArrayOf(3, 2, 4)
val target = 6
// val nums = intArrayOf(3, 3)
// val target = 6
twoSum(nums, target).forEach {
print(" $it")
}
}
fun twoSum(nums: IntArray, target: Int): IntArray {
val map = mutableMapOf<Int, Int>()
nums.forEachIndexed { index, i ->
map[i]?.let {
return intArrayOf(it, index)
}
map[target - i] = index
}
return intArrayOf()
}
My link is described that I am debugging the code. My question is how
map[i]?.let {
return intArrayOf(it, index)
}
is going inside the 1st and 2nd iteration of return
statment and it not going in 3rd iteration. Can anyone help me on this. ThanksThomas Urbanitsch
09/07/2022, 12:55 PMKotlinLeaner
09/07/2022, 12:56 PMKotlinLeaner
09/07/2022, 12:57 PMmap[i]?
is null in 1st iteration then how should it be going inside the lambda and call intArrayOf(it, index)
?KotlinLeaner
09/07/2022, 12:57 PMThomas Urbanitsch
09/07/2022, 2:31 PMThomas Urbanitsch
09/07/2022, 2:47 PMKotlinLeaner
09/07/2022, 2:54 PM