219. Contains Duplicate II
文章目录
Total Accepted: 88264 Total Submissions: 281930 Difficulty: Easy Contributors: Admin
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.
Hide Company Tags Palantir Airbnb Hide Tags Array Hash Table Hide Similar Problems (E) Contains Duplicate (M) Contains Duplicate III
Explanation:
It iterates over the array using a sliding window. The front of the window is at i, the rear of the window is k steps back. The elements within that window are maintained using a set. While adding new element to the set, if add() returns false, it means the element already exists in the set. At that point, we return true. If the control reaches out of for loop, it means that inner return true never executed, meaning no such duplicate element was found.
Reference - simple-java-solution
|
|
文章作者 Hustbill
上次更新 2016-12-18