forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Two New Problems "Contains Duplicate II/III"
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Source : https://leetcode.com/problems/contains-duplicate-ii/ | ||
// Author : Hao Chen | ||
// Date : 2015-06-12 | ||
|
||
/********************************************************************************** | ||
* | ||
* Given an array of integers and an integer k, find out whether there 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. | ||
* | ||
* | ||
**********************************************************************************/ | ||
|
||
class Solution { | ||
public: | ||
bool containsNearbyDuplicate(vector<int>& nums, int k) { | ||
unordered_map<int, int> m; | ||
for (int i=0; i<nums.size(); i++) { | ||
int n = nums[i]; | ||
if (m.find(n) != m.end() && i - m[n] <= k) { | ||
return true; | ||
} | ||
m[n] = i; | ||
} | ||
return false; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Source : https://leetcode.com/problems/contains-duplicate-iii/ | ||
// Author : Hao Chen | ||
// Date : 2015-06-12 | ||
|
||
/********************************************************************************** | ||
* | ||
* Given an array of integers, find out whether there are two distinct indices i and j | ||
* in the array such that the difference between nums[i] and nums[j] is at most t and | ||
* the difference between i and j is at most k. | ||
* | ||
**********************************************************************************/ | ||
|
||
|
||
|
||
class Solution { | ||
public: | ||
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { | ||
if(nums.size() < 2 || k == 0) return false; | ||
int low = 0; | ||
//maintain a sliding window | ||
set<long long> window; | ||
for (int i=0; i<nums.size(); i++){ | ||
//make sure window size <= k | ||
if (i - low > k) { | ||
window.erase(nums[low]); | ||
low++; | ||
} | ||
|
||
// lower_bound() is the key, | ||
// it returns an iterator pointing to the first element >= val | ||
auto it = window.lower_bound((long long)nums[i] - (long long)t ); | ||
if (it != window.end() && abs((long long)nums[i] - *it) <= (long long)t) { | ||
return true; | ||
} | ||
window.insert(nums[i]); | ||
} | ||
return false; | ||
} | ||
}; | ||
|