Skip to content

Commit ebe3226

Browse files
refactor 380
1 parent 2d0a81b commit ebe3226

File tree

1 file changed

+13
-101
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+13
-101
lines changed

src/main/java/com/fishercoder/solutions/_380.java

Lines changed: 13 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -13,131 +13,43 @@ public static class Solution1 {
1313
* credit: https://leetcode.com/problems/insert-delete-getrandom-o1/discuss/85401/Java-solution-using-a-HashMap-and-an-ArrayList-along-with-a-follow-up.-(131-ms)
1414
*/
1515
public static class RandomizedSet {
16-
Map<Integer, Integer> locationMap;
16+
Map<Integer, Integer> map;
1717
List<Integer> list;
1818
Random random;
1919

20-
/**
21-
* Initialize your data structure here.
22-
*/
2320
public RandomizedSet() {
24-
locationMap = new HashMap<>();
25-
list = new ArrayList<>();
21+
map = new HashMap<>();
2622
random = new Random();
23+
list = new ArrayList<>();
2724
}
2825

29-
/**
30-
* Inserts a value to the set. Returns true if the set did not already contain the specified element.
31-
*/
3226
public boolean insert(int val) {
33-
if (locationMap.containsKey(val)) {
27+
if (map.containsKey(val)) {
3428
return false;
35-
} else {
36-
locationMap.put(val, list.size());
37-
list.add(val);
38-
return true;
3929
}
30+
map.put(val, list.size());
31+
list.add(list.size(), val);
32+
return true;
4033
}
4134

42-
/**
43-
* Removes a value from the set. Returns true if the set contained the specified element.
44-
*/
4535
public boolean remove(int val) {
46-
if (!locationMap.containsKey(val)) {
36+
if (!map.containsKey(val)) {
4737
return false;
4838
} else {
49-
int location = locationMap.get(val);
50-
/**if it's not the last one, then swap the last one with this val*/
51-
if (location < list.size() - 1) {
52-
int lastOne = list.get(list.size() - 1);
53-
list.set(location, lastOne);
54-
locationMap.put(lastOne, location);
55-
}
56-
locationMap.remove(val);
39+
int lastElement = list.get(list.size() - 1);
40+
int index = map.get(val);
41+
list.set(index, lastElement);
42+
map.put(lastElement, index);
5743
list.remove(list.size() - 1);
44+
map.remove(val);
5845
return true;
5946
}
6047
}
6148

62-
/**
63-
* Get a random element from the set.
64-
*/
6549
public int getRandom() {
6650
return list.get(random.nextInt(list.size()));
6751
}
68-
6952
}
7053
}
7154

72-
/**
73-
* Your _380 object will be instantiated and called as such:
74-
* _380 obj = new _380();
75-
* boolean param_1 = obj.insert(val);
76-
* boolean param_2 = obj.delete(val);
77-
* int param_3 = obj.getRandom();
78-
*/
79-
public static class Solution2 {
80-
/**This is not ideal and not meeting average O(1) time for all three operations.*/
81-
public static class RandomizedSet {
82-
83-
Map<Integer, Integer> forwardMap;
84-
//key is auto increment index, value if the inserted val
85-
Map<Integer, Integer> reverseMap;//the other way around
86-
int index;
87-
Random random;
88-
89-
/**
90-
* Initialize your data structure here.
91-
*/
92-
public RandomizedSet() {
93-
forwardMap = new HashMap();
94-
reverseMap = new HashMap();
95-
index = 0;
96-
random = new Random();
97-
}
98-
99-
/**
100-
* Inserts a value to the set. Returns true if the set did not already contain the specified
101-
* element.
102-
*/
103-
public boolean insert(int val) {
104-
if (forwardMap.containsValue(val)) {
105-
return false;
106-
} else {
107-
forwardMap.put(index, val);
108-
reverseMap.put(val, index++);
109-
return true;
110-
}
111-
}
112-
113-
/**
114-
* Deletes a value from the set. Returns true if the set contained the specified element.
115-
*/
116-
public boolean remove(int val) {
117-
if (forwardMap.containsValue(val)) {
118-
int key = reverseMap.get(val);
119-
reverseMap.remove(val);
120-
forwardMap.remove(key);
121-
return true;
122-
} else {
123-
return false;
124-
}
125-
}
126-
127-
/**
128-
* Get a random element from the set.
129-
*/
130-
public int getRandom() {
131-
int max = forwardMap.size();
132-
if (max == 1) {
133-
return forwardMap.get(index - 1);
134-
}
135-
int randomNum = random.nextInt(max);
136-
while (!forwardMap.containsKey(randomNum)) {
137-
randomNum = random.nextInt(max);
138-
}
139-
return forwardMap.get(randomNum);
140-
}
141-
}
142-
}
14355
}

0 commit comments

Comments
 (0)