File tree Expand file tree Collapse file tree 3 files changed +70
-0
lines changed
algorithms/containsDuplicate Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change 77
88| # | Title | Solution | Difficulty |
99| ---| ----- | -------- | ---------- |
10+ | 204| [ Contains Duplicate III] ( https://leetcode.com/problems/contains-duplicate-iii/ ) | [ C++] ( ./algorithms/containsDuplicate/ContainsDuplicate.III.cpp ) | Medium|
11+ | 203| [ Contains Duplicate II] ( https://leetcode.com/problems/contains-duplicate-ii/ ) | [ C++] ( ./algorithms/containsDuplicate/ContainsDuplicate.II.cpp ) | Easy|
1012| 202| [ The Skyline Problem] ( https://leetcode.com/problems/the-skyline-problem/ ) | [ C++] ( ./algorithms/theSkylineProblem/TheSkylineProblem.cpp ) | Hard|
1113| 201| [ Contains Duplicate] ( https://leetcode.com/problems/contains-duplicate/ ) | [ C++] ( ./algorithms/containsDuplicate/ContainsDuplicate.cpp ) | Easy|
1214| 200| [ Combination Sum III] ( https://leetcode.com/problems/combination-sum-iii/ ) | [ C++] ( ./algorithms/combinationSum/combinationSum.III.cpp ) | Medium|
Original file line number Diff line number Diff line change 1+ // Source : https://leetcode.com/problems/contains-duplicate-ii/
2+ // Author : Hao Chen
3+ // Date : 2015-06-12
4+
5+ /* *********************************************************************************
6+ *
7+ * Given an array of integers and an integer k, find out whether there there are
8+ * two distinct indices i and j in the array such that nums[i] = nums[j] and
9+ * the difference between i and j is at most k.
10+ *
11+ *
12+ **********************************************************************************/
13+
14+ class Solution {
15+ public:
16+ bool containsNearbyDuplicate (vector<int >& nums, int k) {
17+ unordered_map<int , int > m;
18+ for (int i=0 ; i<nums.size (); i++) {
19+ int n = nums[i];
20+ if (m.find (n) != m.end () && i - m[n] <= k) {
21+ return true ;
22+ }
23+ m[n] = i;
24+ }
25+ return false ;
26+ }
27+ };
28+
Original file line number Diff line number Diff line change 1+ // Source : https://leetcode.com/problems/contains-duplicate-iii/
2+ // Author : Hao Chen
3+ // Date : 2015-06-12
4+
5+ /* *********************************************************************************
6+ *
7+ * Given an array of integers, find out whether there are two distinct indices i and j
8+ * in the array such that the difference between nums[i] and nums[j] is at most t and
9+ * the difference between i and j is at most k.
10+ *
11+ **********************************************************************************/
12+
13+
14+
15+ class Solution {
16+ public:
17+ bool containsNearbyAlmostDuplicate (vector<int >& nums, int k, int t) {
18+ if (nums.size () < 2 || k == 0 ) return false ;
19+ int low = 0 ;
20+ // maintain a sliding window
21+ set<long long > window;
22+ for (int i=0 ; i<nums.size (); i++){
23+ // make sure window size <= k
24+ if (i - low > k) {
25+ window.erase (nums[low]);
26+ low++;
27+ }
28+
29+ // lower_bound() is the key,
30+ // it returns an iterator pointing to the first element >= val
31+ auto it = window.lower_bound ((long long )nums[i] - (long long )t );
32+ if (it != window.end () && abs ((long long )nums[i] - *it) <= (long long )t) {
33+ return true ;
34+ }
35+ window.insert (nums[i]);
36+ }
37+ return false ;
38+ }
39+ };
40+
You can’t perform that action at this time.
0 commit comments