@@ -13,131 +13,43 @@ public static class Solution1 {
13
13
* 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)
14
14
*/
15
15
public static class RandomizedSet {
16
- Map <Integer , Integer > locationMap ;
16
+ Map <Integer , Integer > map ;
17
17
List <Integer > list ;
18
18
Random random ;
19
19
20
- /**
21
- * Initialize your data structure here.
22
- */
23
20
public RandomizedSet () {
24
- locationMap = new HashMap <>();
25
- list = new ArrayList <>();
21
+ map = new HashMap <>();
26
22
random = new Random ();
23
+ list = new ArrayList <>();
27
24
}
28
25
29
- /**
30
- * Inserts a value to the set. Returns true if the set did not already contain the specified element.
31
- */
32
26
public boolean insert (int val ) {
33
- if (locationMap .containsKey (val )) {
27
+ if (map .containsKey (val )) {
34
28
return false ;
35
- } else {
36
- locationMap .put (val , list .size ());
37
- list .add (val );
38
- return true ;
39
29
}
30
+ map .put (val , list .size ());
31
+ list .add (list .size (), val );
32
+ return true ;
40
33
}
41
34
42
- /**
43
- * Removes a value from the set. Returns true if the set contained the specified element.
44
- */
45
35
public boolean remove (int val ) {
46
- if (!locationMap .containsKey (val )) {
36
+ if (!map .containsKey (val )) {
47
37
return false ;
48
38
} 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 );
57
43
list .remove (list .size () - 1 );
44
+ map .remove (val );
58
45
return true ;
59
46
}
60
47
}
61
48
62
- /**
63
- * Get a random element from the set.
64
- */
65
49
public int getRandom () {
66
50
return list .get (random .nextInt (list .size ()));
67
51
}
68
-
69
52
}
70
53
}
71
54
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
- }
143
55
}
0 commit comments