Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ContainsDuplicatesII_219 {
- public static void main(String[] args) {
- int[] arr = new int[] {99,99};
- int k = 2;
- System.out.println(ContainsDuplicatesII_219.containsNearbyDuplicate(arr, k));
- }
- public static boolean containsNearbyDuplicate(int[] nums, int k) {
- ArrayList<Integer> result = new ArrayList<Integer>();
- int windowStart = 0;
- for (int windowEnd = 0; windowEnd < nums.length; windowEnd++) {
- result.add(nums[windowEnd]);
- if (result.size() >= k) {
- if (result.get(windowStart) == result.get(result.size() - 1) && Math.abs(result.get(windowStart) - result.get(result.size() - 1)) <= k) {
- return true;
- }
- result.remove(windowStart);
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement