@@ -35,78 +35,73 @@ Design a data structure that supports all following operations in average O(1) t
35
35
// getRandom should return 1 and 2 both equally likely.
36
36
collection.getRandom();*/
37
37
public class _381 {
38
-
39
- Map <Integer , Integer > forwardMap ;//key is the to-be-inserted number, value is its auto-incremented index
40
- Map <Integer , Integer > reverseMap ;//the other way around
41
- int index ;
42
- Random rand ;
43
-
44
- /**
45
- * Initialize your data structure here.
46
- */
47
- public _381 () {
48
- forwardMap = new HashMap ();
49
- reverseMap = new HashMap ();
50
- index = 0 ;
51
- rand = new Random ();
52
- }
53
-
54
- /**
55
- * Inserts a value to the collection. Returns true if the collection did not already contain the specified element.
56
- */
57
- public boolean insert (int val ) {
58
- boolean contains ;
59
- if (reverseMap .containsValue (val )) {
60
- contains = true ;
61
- } else {
62
- contains = false ;
38
+ public static class Solution1 {
39
+
40
+ Map <Integer , Integer > forwardMap ;
41
+ //key is the to-be-inserted number, value is its auto-incremented index
42
+ Map <Integer , Integer > reverseMap ;//the other way around
43
+ int index ;
44
+ Random rand ;
45
+
46
+ /**
47
+ * Initialize your data structure here.
48
+ */
49
+ public _381 () {
50
+ forwardMap = new HashMap ();
51
+ reverseMap = new HashMap ();
52
+ index = 0 ;
53
+ rand = new Random ();
63
54
}
64
- forwardMap .put (val , index );//this will overwrite the preivous key with a new index if the key already exists
65
- reverseMap .put (index , val );
66
- index ++;
67
- return contains ;
68
- }
69
55
70
- /**
71
- * Removes a value from the collection. Returns true if the collection contained the specified element.
72
- */
73
- public boolean remove (int val ) {
74
- boolean contains ;
75
- if (reverseMap .containsValue (val )) {
76
- contains = true ;
77
- if (forwardMap .containsKey (val )) {
78
- int i = forwardMap .get (val );
79
- forwardMap .remove (val );
80
- reverseMap .remove (i );
56
+ /**
57
+ * Inserts a value to the collection. Returns true if the collection did not already contain the
58
+ * specified element.
59
+ */
60
+ public boolean insert (int val ) {
61
+ boolean contains ;
62
+ if (reverseMap .containsValue (val )) {
63
+ contains = true ;
81
64
} else {
82
- //remove the entry in revserve map that has val as its value
83
- reverseMap .values ().remove (val );
65
+ contains = false ;
84
66
}
85
- } else {
86
- contains = false ;
67
+ forwardMap .put (val ,
68
+ index );//this will overwrite the preivous key with a new index if the key already exists
69
+ reverseMap .put (index , val );
70
+ index ++;
71
+ return contains ;
87
72
}
88
- return contains ;
89
- }
90
73
91
- /**
92
- * Get a random element from the collection.
93
- */
94
- public int getRandom () {
95
- int randNum = rand .nextInt (index );
96
- while (!reverseMap .containsKey (randNum )) {
97
- randNum = rand .nextInt (index );
74
+ /**
75
+ * Removes a value from the collection. Returns true if the collection contained the specified
76
+ * element.
77
+ */
78
+ public boolean remove (int val ) {
79
+ boolean contains ;
80
+ if (reverseMap .containsValue (val )) {
81
+ contains = true ;
82
+ if (forwardMap .containsKey (val )) {
83
+ int i = forwardMap .get (val );
84
+ forwardMap .remove (val );
85
+ reverseMap .remove (i );
86
+ } else {
87
+ //remove the entry in revserve map that has val as its value
88
+ reverseMap .values ().remove (val );
89
+ }
90
+ } else {
91
+ contains = false ;
92
+ }
93
+ return contains ;
98
94
}
99
- return reverseMap .get (randNum );
100
- }
101
95
102
- public static void main (String ... strings ) {
103
- _381 test = new _381 ();
104
- System .out .println (test .insert (1 ));
105
- System .out .println (test .insert (1 ));
106
- System .out .println (test .insert (2 ));
107
- System .out .println (test .getRandom ());
108
- System .out .println (test .remove (1 ));
109
- System .out .println (test .getRandom ());
96
+ /**
97
+ * Get a random element from the collection.
98
+ */
99
+ public int getRandom () {
100
+ int randNum = rand .nextInt (index );
101
+ while (!reverseMap .containsKey (randNum )) {
102
+ randNum = rand .nextInt (index );
103
+ }
104
+ return reverseMap .get (randNum );
105
+ }
110
106
}
111
-
112
107
}
0 commit comments