Skip to content

Commit 15c67d0

Browse files
committed
fd
1 parent 860ff8b commit 15c67d0

File tree

4 files changed

+28
-39
lines changed

4 files changed

+28
-39
lines changed

solution/src/main/java/com/inuker/solution/ContainsDuplicateIII.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,7 @@ public boolean containsNearbyAlmostDuplicate2(int[] nums, int k, int t) {
6161
Map<Long, Long> map = new HashMap<>();
6262

6363
for (int i = 0; i < nums.length; i++) {
64-
long index;
65-
66-
if (i >= k + 1) {
67-
index = getBucketId(nums[i - k - 1], (long) t + 1);
68-
map.remove(index);
69-
}
70-
71-
index = getBucketId(nums[i], (long) t + 1);
64+
long index = getBucketId(nums[i], (long) t + 1);
7265

7366
if (map.containsKey(index)) {
7467
return true;
@@ -83,6 +76,11 @@ public boolean containsNearbyAlmostDuplicate2(int[] nums, int k, int t) {
8376
}
8477

8578
map.put(index, (long) nums[i]);
79+
80+
if (i >= k) {
81+
index = getBucketId(nums[i - k], (long) t + 1);
82+
map.remove(index);
83+
}
8684
}
8785

8886
return false;

solution/src/main/java/com/inuker/solution/IntersectionOfTwoArraysII.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,20 @@ public class IntersectionOfTwoArraysII {
1111

1212
public int[] intersect(int[] nums1, int[] nums2) {
1313
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
14-
ArrayList<Integer> result = new ArrayList<Integer>();
15-
for(int i = 0; i < nums1.length; i++)
16-
{
17-
if(map.containsKey(nums1[i])) map.put(nums1[i], map.get(nums1[i])+1);
18-
else map.put(nums1[i], 1);
14+
for (int i = 0; i < nums1.length; i++) {
15+
map.put(nums1[i], map.getOrDefault(nums1[i], 0) + 1);
1916
}
20-
21-
for(int i = 0; i < nums2.length; i++)
22-
{
23-
if(map.containsKey(nums2[i]) && map.get(nums2[i]) > 0)
24-
{
17+
ArrayList<Integer> result = new ArrayList<Integer>();
18+
for (int i = 0; i < nums2.length; i++) {
19+
if (map.getOrDefault(nums2[i], 0) > 0) {
2520
result.add(nums2[i]);
26-
map.put(nums2[i], map.get(nums2[i])-1);
21+
map.put(nums2[i], map.get(nums2[i]) - 1);
2722
}
2823
}
29-
3024
int[] r = new int[result.size()];
31-
for(int i = 0; i < result.size(); i++)
32-
{
25+
for (int i = 0; i < result.size(); i++) {
3326
r[i] = result.get(i);
3427
}
35-
3628
return r;
3729
}
3830
}

solution/src/main/java/com/inuker/solution/MaximumGap.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88

99
public class MaximumGap {
1010

11+
/**
12+
* 这题核心是最大gap存在于跨桶的,而不是某个桶内部
13+
* 首先刨去min和max,剩余n-2个数分散在n-1个桶中,必然导致有的桶里是空的
14+
* 那么max gap必然是跨桶的
15+
*/
1116
// 耗时5ms,木桶原理
1217
public int maximumGap(int[] nums) {
1318
if (nums.length < 2) {
1419
return 0;
1520
}
1621

1722
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
18-
/**
19-
* 首先找出数组中最大值和最小值
20-
*/
23+
2124
for (int n : nums) {
2225
min = Math.min(min, n);
2326
max = Math.max(max, n);
@@ -27,32 +30,29 @@ public int maximumGap(int[] nums) {
2730
return 0;
2831
}
2932

30-
/**
31-
* 有len个数,则有len-1个间隙,这里算出平均每个桶的宽度,桶的个数是len-1
32-
*/
33-
int gap = (int) Math.ceil((double) (max - min) / (nums.length - 1));
34-
int[] mins = new int[nums.length - 1], maxs = new int[nums.length - 1];
33+
int bucketCount = nums.length - 1;
34+
int gap = (int) Math.ceil((double) (max - min) / bucketCount);
35+
int[] mins = new int[bucketCount], maxs = new int[bucketCount];
3536

3637
Arrays.fill(mins, Integer.MAX_VALUE);
3738
Arrays.fill(maxs, Integer.MIN_VALUE);
3839

3940
/**
40-
* 这里要统计落在每个桶内的最大值和最小值
41+
* 注意区间是左闭右开的,所以max没有包含进来,这里先去掉,最后再算
42+
* 如果这里不略过max的话,算出来的index会越界
4143
*/
4244
for (int n : nums) {
43-
if (n == min || n == max) {
45+
if (n == max) {
4446
continue;
4547
}
46-
/**
47-
* 先算出n所在的桶
48-
*/
4948
int index = (n - min) / gap;
5049
mins[index] = Math.min(mins[index], n);
5150
maxs[index] = Math.max(maxs[index], n);
5251
}
5352

54-
int last = min, maxGap = Integer.MIN_VALUE;
55-
for (int i = 0; i < nums.length - 1; i++) {
53+
int last = min, maxGap = 0;
54+
for (int i = 0; i < bucketCount; i++) {
55+
// 略过空的桶
5656
if (mins[i] == Integer.MAX_VALUE) {
5757
continue;
5858
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@
2727
public class main {
2828

2929
public static void main(String[] args) {
30-
3130
}
3231
}

0 commit comments

Comments
 (0)