?LeetCode刷題實戰(zhàn)220:存在重復(fù)元素 III
Given an integer array nums and two integers k and t, return true if there are two distinct indices i and j in the array such that abs(nums[i] - nums[j]) <= t and abs(i - j) <= k.
示例
示例 1:
輸入: nums = [1,2,3,1], k = 3, t = 0
輸出: true
示例 2:
輸入: nums = [1,0,1,1], k = 1, t = 2
輸出: true
示例 3:
輸入: nums = [1,5,9,1,5,9], k = 2, t = 3
輸出: false
解題
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
set<long> s;
for(int i = 0; i < nums.size(); i++) {
auto it = s.lower_bound(nums[i] - long(t));
if(it != s.end() && *it - nums[i] <= t) {
return true;
}
s.insert(nums[i]);
if(s.size() > k) {
s.erase(nums[i - k]);
}
}
return false;
}
};
LeetCode刷題實戰(zhàn)201:數(shù)字范圍按位與
LeetCode刷題實戰(zhàn)202:快樂數(shù)
LeetCode刷題實戰(zhàn)204:計數(shù)質(zhì)數(shù)
LeetCode刷題實戰(zhàn)205:同構(gòu)字符串
LeetCode刷題實戰(zhàn)206:反轉(zhuǎn)鏈表
LeetCode刷題實戰(zhàn)208:實現(xiàn) Trie (前綴樹)
LeetCode刷題實戰(zhàn)209:長度最小的子數(shù)組
LeetCode刷題實戰(zhàn)215:數(shù)組中的第K個最大元素
LeetCode刷題實戰(zhàn)216:組合總和 III
LeetCode刷題實戰(zhàn)217:存在重復(fù)元素
