-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainsDuplicateIII.java
49 lines (38 loc) · 1.21 KB
/
ContainsDuplicateIII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package arrays;
public class ContainsDuplicateIII {
public static void main(String[] args) {
ContainsDuplicateIII c= new ContainsDuplicateIII();
/* System.out.println(
c.containsNearbyAlmostDuplicate(new int[]{1,2,3,1}, 3,0)
);
System.out.println(
c.containsNearbyAlmostDuplicate(new int[]{1,0,1,1}, 1,2)
);
System.out.println(
c.containsNearbyAlmostDuplicate(new int[]{1,5,9,1,5,9}, 2,3)
);*/
System.out.println(
c.containsNearbyAlmostDuplicate(new int[]{0}, 0,0)
);
}
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
for(int i =0;i<nums.length-1;i++){
int pos = i+k;
if(pos>nums.length-1){
pos=nums.length-1;
}
for(int j=pos;j>i;j--){
long val1 = (long) nums[j];
long val2 = (long) nums[i];
long result = val1-val2;
if(result<0){
result = result*-1;
}
if(result<=t){
return true;
}
}
}
return false;
}
}