g0201_0300.s0219_contains_duplicate_ii.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0201_0300.s0219_contains_duplicate_ii
// #Easy #Array #Hash_Table #Sliding_Window #2022_10_25_Time_813_ms_(80.46%)_Space_71.1_MB_(81.22%)
class Solution {
fun containsNearbyDuplicate(nums: IntArray, k: Int): Boolean {
val map: MutableMap = HashMap()
val len = nums.size
for (i in 0 until len) {
val index = map.put(nums[i], i)
if (index != null && Math.abs(index - i) <= k) {
return true
}
}
return false
}
}