@@ -46,64 +46,68 @@ public class _380 {
46
46
* boolean param_2 = obj.delete(val);
47
47
* int param_3 = obj.getRandom();
48
48
*/
49
- //TODO: this is not ideal, optimize it.
50
- public static class RandomizedSet {
49
+ public static class Solution1 {
50
+ //TODO: this is not ideal, optimize it.
51
+ public static class RandomizedSet {
51
52
52
- Map <Integer , Integer > forwardMap ;//key is auto increment index, value if the inserted val
53
- Map <Integer , Integer > reverseMap ;//the other way around
54
- int index ;
55
- Random random ;
53
+ Map <Integer , Integer > forwardMap ;
54
+ //key is auto increment index, value if the inserted val
55
+ Map <Integer , Integer > reverseMap ;//the other way around
56
+ int index ;
57
+ Random random ;
56
58
57
- /**
58
- * Initialize your data structure here.
59
- */
60
- public RandomizedSet () {
61
- forwardMap = new HashMap ();
62
- reverseMap = new HashMap ();
63
- index = 0 ;
64
- random = new Random ();
65
- }
66
-
67
- /**
68
- * Inserts a value to the set. Returns true if the set did not already contain the specified element.
69
- */
70
- public boolean insert (int val ) {
71
- if (forwardMap .containsValue (val )) {
72
- return false ;
73
- } else {
74
- forwardMap .put (index , val );
75
- reverseMap .put (val , index ++);
76
- return true ;
59
+ /**
60
+ * Initialize your data structure here.
61
+ */
62
+ public RandomizedSet () {
63
+ forwardMap = new HashMap ();
64
+ reverseMap = new HashMap ();
65
+ index = 0 ;
66
+ random = new Random ();
77
67
}
78
- }
79
68
80
- /**
81
- * Deletes a value from the set. Returns true if the set contained the specified element.
82
- */
83
- public boolean remove (int val ) {
84
- if (forwardMap .containsValue (val )) {
85
- int key = reverseMap .get (val );
86
- reverseMap .remove (val );
87
- forwardMap .remove (key );
88
- return true ;
89
- } else {
90
- return false ;
69
+ /**
70
+ * Inserts a value to the set. Returns true if the set did not already contain the specified
71
+ * element.
72
+ */
73
+ public boolean insert (int val ) {
74
+ if (forwardMap .containsValue (val )) {
75
+ return false ;
76
+ } else {
77
+ forwardMap .put (index , val );
78
+ reverseMap .put (val , index ++);
79
+ return true ;
80
+ }
91
81
}
92
- }
93
82
94
- /**
95
- * Get a random element from the set.
96
- */
97
- public int getRandom () {
98
- int max = forwardMap .size ();
99
- if (max == 1 ) {
100
- return forwardMap .get (index - 1 );
83
+ /**
84
+ * Deletes a value from the set. Returns true if the set contained the specified element.
85
+ */
86
+ public boolean remove (int val ) {
87
+ if (forwardMap .containsValue (val )) {
88
+ int key = reverseMap .get (val );
89
+ reverseMap .remove (val );
90
+ forwardMap .remove (key );
91
+ return true ;
92
+ } else {
93
+ return false ;
94
+ }
101
95
}
102
- int randomNum = random .nextInt (max );
103
- while (!forwardMap .containsKey (randomNum )) {
104
- randomNum = random .nextInt (max );
96
+
97
+ /**
98
+ * Get a random element from the set.
99
+ */
100
+ public int getRandom () {
101
+ int max = forwardMap .size ();
102
+ if (max == 1 ) {
103
+ return forwardMap .get (index - 1 );
104
+ }
105
+ int randomNum = random .nextInt (max );
106
+ while (!forwardMap .containsKey (randomNum )) {
107
+ randomNum = random .nextInt (max );
108
+ }
109
+ return forwardMap .get (randomNum );
105
110
}
106
- return forwardMap .get (randomNum );
107
111
}
108
112
}
109
- }
113
+ }
0 commit comments