Contains Duplicate II
Question
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k.
Answer
solution:
Knowledge:
这道第题目我还是使用常规的遍历循环去解决,很明显,在时间复杂度上,不能应付很长的列表。
这一类属于two sum一样的下标元素类问题。关键是用enumerate去同时获得下标和元素,并且用辅助的字典记入进去(这就是类似双指针——相邻移动的一种补充技术:更新变量减少指针。),然后进行查找是否存在已有值。
Last updated
Was this helpful?