Java for LeetCode 220 Contains Duplicate III

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.

解题思路:

暴力枚举会LTE,解题思路是用sortedSet来解决,sortedSet可以返回一个在某个范围的subSet,同时判断这个subSet是否为空即可解决,主要,本题需要使用long类型!

JAVA实现如下:

import java.util.*;
public class Solution {
    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if(nums==null || nums.length<2||k<1 || t<0)
        	return false;
        SortedSet<Long> set = new TreeSet<Long>();
        for(int j=0; j<nums.length; j++) {
            SortedSet<Long> subSet =  set.subSet((long)nums[j]-t, (long)nums[j]+t+1);
            if(!subSet.isEmpty())
            	return true;
            if(j>=k)
                set.remove((long)nums[j-k]);
            set.add((long)nums[j]);
        }
        return false;
    }
}
时间: 2024-10-29 03:28:22

Java for LeetCode 220 Contains Duplicate III的相关文章

leetcode 220. Contains Duplicate III 求一个数组中有没有要求的元素 ---------- java

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. 找出数组中有没有最多相距k,同时最大相差

[LeetCode] 220. Contains Duplicate III Java

题目: Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k. 题意及分析: 给出一个数组,要求

(medium)LeetCode 220.Contains Duplicate III

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. 思想借鉴:维持一个长度为k的window, 每次检查新的值是否与原来窗口中的

Leetcode 220 Contains Duplicate III

1. 问题描述 给定一个整数数组nums[],查找是否存在两个下标i和j,满足|numsi?numsj|≤t 且 |i?j|≤k. 2. 方法与思路 总得思路就是:"滑动窗口"+unordered_map. 推理过程如下: |numsi?numsj|≤t?|numsi/t?numsj/t|≤1: 由上式可以推出:|?numsi/t???numsj/t?|≤1 等价于:?numsi/t?∈{?numsj/t?,?numsj/t??1,?numsj/t?+1}. 我们只需要维持一个大小为K

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

【LeetCode】220. Contains Duplicate III

题目: 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. 提示: 看到题目之后,马上想到了题目应该是要利用一个长度为k的划窗,

Java for LeetCode 219 Contains Duplicate II

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. 解题思路: HashMap,JAVA实现如下: public boolean containsNearby

Java for LeetCode 217 Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 解题思路: HashMap,JAVA实现如下: public boolean

220. Contains Duplicate III

1 class Solution { 2 public: 3 bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { 4 if (nums.size() < 2) return false; 5 multimap<int, int> m; 6 for (int i = 0; i < nums.size(); ++i) { 7 m.insert(pair<int, int&g