Skip to content

Commit 93e0c52

Browse files
committed
fd
1 parent 219b4df commit 93e0c52

File tree

3 files changed

+85
-42
lines changed

3 files changed

+85
-42
lines changed

leetcode/solution/src/InsertDeleteGetRandom.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,47 @@
1010
// 耗时111ms
1111
public class InsertDeleteGetRandom {
1212

13-
private HashMap<Integer, Integer> mMap;
14-
private List<Integer> mList;
15-
private Random mRandom;
13+
ArrayList<Integer> list;
14+
HashMap<Integer, Integer> map;
15+
Random random;
1616

17+
/** Initialize your data structure here. */
1718
public InsertDeleteGetRandom() {
18-
mList = new ArrayList<Integer>();
19-
mMap = new HashMap<Integer, Integer>();
20-
mRandom = new Random();
19+
map = new HashMap<>();
20+
list = new ArrayList<>();
21+
random = new Random();
2122
}
2223

24+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
2325
public boolean insert(int val) {
24-
if (mMap.containsKey(val)) {
26+
if (map.containsKey(val)) {
2527
return false;
2628
}
27-
mList.add(val);
28-
mMap.put(val, mList.size() - 1);
29+
list.add(val);
30+
map.put(val, list.size() - 1);
2931
return true;
3032
}
3133

34+
/** Removes a value from the set. Returns true if the set contained the specified element. */
3235
public boolean remove(int val) {
33-
if (!mMap.containsKey(val)) {
36+
int index = map.getOrDefault(val, -1);
37+
if (index < 0) {
3438
return false;
3539
}
36-
int index = mMap.remove(val);
37-
int lastIndex = mList.size() - 1;
38-
if (index != lastIndex) {
39-
int lastVal = mList.get(lastIndex);
40-
mList.set(index, lastVal);
41-
// 这里要注意重新设置lastVal的index
42-
mMap.put(lastVal, index);
40+
if (index != list.size() - 1) {
41+
int tail = list.get(list.size() - 1);
42+
list.set(index, tail);
43+
map.put(tail, index);
4344
}
44-
mList.remove(lastIndex);
45+
map.remove(val);
46+
list.remove(list.size() - 1);
4547
return true;
4648
}
4749

50+
/** Get a random element from the set. */
4851
public int getRandom() {
49-
int index = mRandom.nextInt(mList.size());
50-
return mList.get(index);
52+
int index = random.nextInt(list.size());
53+
index = (index >= 0 ? index : -index);
54+
return list.get(index);
5155
}
5256
}

leetcode/solution/src/ThreeSumClosest.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,25 @@ public class ThreeSumClosest {
55
public int threeSumClosest(int[] nums, int target) {
66
Arrays.sort(nums);
77

8-
long dis = Integer.MAX_VALUE, result = 0;
9-
10-
for (int i = 0; i < nums.length - 2; i++) {
11-
int newTarget = target - nums[i];
12-
13-
for (int j = i + 1, k = nums.length - 1; j < k; ) {
14-
int sum = nums[j] + nums[k];
15-
16-
if (sum > newTarget) {
17-
k--;
18-
} else if (sum < newTarget) {
19-
j++;
8+
int min = Integer.MAX_VALUE, res = 0;
9+
for (int k = 0; k < nums.length - 2; k++) {
10+
11+
for (int i = k + 1, j = nums.length - 1; i < j; ) {
12+
int sum = nums[k] + nums[i] + nums[j];
13+
if (sum > target) {
14+
j--;
15+
} else if (sum < target) {
16+
i++;
2017
} else {
21-
return target;
18+
return sum;
2219
}
23-
24-
long delta = Math.abs(newTarget - sum);
25-
if (delta < dis) {
26-
dis = delta;
27-
result = sum + nums[i];
20+
int delta = Math.abs(sum - target);
21+
if (delta < min) {
22+
min = delta;
23+
res = sum;
2824
}
2925
}
3026
}
31-
32-
return (int) result;
27+
return res;
3328
}
3429
}

leetcode/src/Main.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,54 @@ public static class Solution {
77

88
}
99

10+
class RandomizedSet {
11+
12+
ArrayList<Integer> list;
13+
HashMap<Integer, Integer> map;
14+
Random random;
15+
16+
/** Initialize your data structure here. */
17+
public RandomizedSet() {
18+
map = new HashMap<>();
19+
list = new ArrayList<>();
20+
random = new Random();
21+
}
22+
23+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
24+
public boolean insert(int val) {
25+
if (map.containsKey(val)) {
26+
return false;
27+
}
28+
list.add(val);
29+
map.put(val, list.size() - 1);
30+
return true;
31+
}
32+
33+
/** Removes a value from the set. Returns true if the set contained the specified element. */
34+
public boolean remove(int val) {
35+
int index = map.getOrDefault(val, -1);
36+
if (index < 0) {
37+
return false;
38+
}
39+
if (index != list.size() - 1) {
40+
int tail = list.get(list.size() - 1);
41+
list.set(index, tail);
42+
map.put(tail, index);
43+
}
44+
map.remove(val);
45+
list.remove(list.size() - 1);
46+
return true;
47+
}
48+
49+
/** Get a random element from the set. */
50+
public int getRandom() {
51+
int index = random.nextInt() % list.size();
52+
index = (index >= 0 ? index : -index);
53+
return list.get(index);
54+
}
55+
}
1056

1157
public static void main(String[] args) {
1258
Solution solution = new Solution();
13-
String s = solution.mostCommonWord("Bob", new String[0]);
14-
System.out.println(s);
1559
}
1660
}

0 commit comments

Comments
 (0)