Created
May 13, 2018 03:35
-
-
Save liweiwei1419/a047eceb6c30525d420d940cf668665b to your computer and use it in GitHub Desktop.
LeetCode 第 220 题题解(Java)。
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
import java.util.TreeSet; | |
// https://leetcode-cn.com/problems/contains-duplicate-iii/description/ | |
public class Solution { | |
/** | |
* @param nums | |
* @param k k 是索引之间的最大差值 | |
* @param t 是两个数值之间的最大差值 | |
* @return | |
*/ | |
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { | |
TreeSet<Long> set = new TreeSet<>(); | |
for (int i = 0; i < nums.length; i++) { | |
Long floor = set.floor((long)nums[i] + t); | |
Long ceiling = set.ceiling((long)nums[i] - t); | |
if (floor != null && nums[i] <= floor || ceiling != null && nums[i] >= ceiling) { | |
return true; | |
} | |
set.add((long)nums[i]); | |
if (set.size() == k+1 ) { | |
set.remove((long)nums[i - k]); | |
} | |
} | |
return false; | |
} | |
public static void main(String[] args) { | |
// write your code here | |
int[] nums = {18,7,21,10}; | |
int k = 2; | |
int t = 2; | |
Solution solution = new Solution(); | |
boolean containsNearbyAlmostDuplicate = solution.containsNearbyAlmostDuplicate(nums, k, t); | |
System.out.println(containsNearbyAlmostDuplicate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment