220:存在重复元素 III
This commit is contained in:
parent
17eea862e0
commit
cce43d9cd6
@ -0,0 +1,71 @@
|
|||||||
|
//给你一个整数数组 nums 和两个整数 k 和 t 。请你判断是否存在 两个不同下标 i 和 j,使得 abs(nums[i] - nums[j]) <=
|
||||||
|
//t ,同时又满足 abs(i - j) <= k 。
|
||||||
|
//
|
||||||
|
// 如果存在则返回 true,不存在返回 false。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 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
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 0 <= nums.length <= 2 * 104
|
||||||
|
// -231 <= nums[i] <= 231 - 1
|
||||||
|
// 0 <= k <= 104
|
||||||
|
// 0 <= t <= 231 - 1
|
||||||
|
//
|
||||||
|
// Related Topics 排序 Ordered Map
|
||||||
|
// 👍 391 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
//220:存在重复元素 III
|
||||||
|
public class ContainsDuplicateIii {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new ContainsDuplicateIii().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
|
||||||
|
int n = nums.length;
|
||||||
|
TreeSet<Long> set = new TreeSet<>();
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
Long ceiling = set.ceiling((long) nums[i] - (long) t);
|
||||||
|
if (ceiling != null && ceiling <= (long) nums[i] + (long) t) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
set.add((long) nums[i]);
|
||||||
|
if (i >= k) {
|
||||||
|
set.remove((long) nums[i - k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
<p>给你一个整数数组 <code>nums</code> 和两个整数 <code>k</code> 和 <code>t</code> 。请你判断是否存在 <b>两个不同下标</b> <code>i</code> 和 <code>j</code>,使得 <code>abs(nums[i] - nums[j]) <= t</code> ,同时又满足 <code>abs(i - j) <= k</code><em> </em>。</p>
|
||||||
|
|
||||||
|
<p>如果存在则返回 <code>true</code>,不存在返回 <code>false</code>。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,2,3,1], k<em> </em>= 3, t = 0
|
||||||
|
<strong>输出:</strong>true</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,0,1,1], k<em> </em>=<em> </em>1, t = 2
|
||||||
|
<strong>输出:</strong>true</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,5,9,1,5,9], k = 2, t = 3
|
||||||
|
<strong>输出:</strong>false</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>0 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||||
|
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||||
|
<li><code>0 <= k <= 10<sup>4</sup></code></li>
|
||||||
|
<li><code>0 <= t <= 2<sup>31</sup> - 1</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>排序</li><li>Ordered Map</li></div></div>\n<div><li>👍 391</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user