[easy][Array][HashTable]219.Contains Duplicate II
原題是:
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 absolute difference between i and j is at most k.
思路是:
陣列中用到hash往往跟強調元素的index有關。這些Index往往是固定不變的。
程式碼是:
class Solution:
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if not nums or k < 1:
return False
dicts = {}
for i,num in enumerate(nums):
if num not in dicts:
dicts[num] = i
else:
if i - dicts[num] <= k:
return True
else:
dicts[num] = i
return False
相關文章
- LeetCode-Contains Duplicate IILeetCodeAI
- Leetcode Search in Rotated Sorted Array IILeetCode
- LeetCode 350 [Intersection of Two Array II]LeetCode
- LeetCode-Search in Rotated Sorted Array IILeetCode
- Leetcode Remove Duplicates from Sorted Array IILeetCodeREM
- Search in Rotated Sorted Array II leetcode javaLeetCodeJava
- leetcode 219. Contains Duplicate IILeetCodeAI
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- Leetcode-Remove Duplicates from Sorted Array IILeetCodeREM
- Remove Duplicates from Sorted Array II leetcode javaREMLeetCodeJava
- Mac重複圖片查詢軟體——Easy Duplicate Photo Finder for MacMac
- Leetcode-Find Minimum in Rotated Sorted Array IILeetCode
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- 【Leetcode】167. Two Sum II - Input array is sortedLeetCode
- init_array與got劫持——[zer0pts 2020]easy strcmpGo
- 深度解析Hashtable
- Leetcode每日一題:992.sort-array-by-parity-ii(按奇偶排序陣列Ⅱ)LeetCode每日一題排序陣列
- Hashtable原始碼分析原始碼
- C++ STL -- HashTableC++
- C#集合類(HashTable, Dictionary, ArrayList)與HashTable執行緒安全C#執行緒
- array new 與 array deletedelete
- duplicate databaseDatabase
- PHP array_flip() array_merge() array+array的使用總結PHP
- HashMap、HashTable、HashSet詳解HashMap
- 【java】【Map】HashMap、Hashtable、CollectionsJavaHashMap
- Hashtable簡介和使用
- hashtable 泛型 C#泛型C#
- Hashtable基礎學習
- c#之hashtable類C#
- c#hashtable 遍歷C#
- c#遍歷HashTableC#
- 字串魔法(easy)字串
- ACM A problem is easyACM
- 【BUUCTF】easy calc
- 【BUUCTF】Easy JavaJava
- array
- Array()與Array.of()方法區別
- RMAN duplicate databaseDatabase