Skip to content

Commit 13ae2b6

Browse files
refactor 381
1 parent ade79b1 commit 13ae2b6

File tree

1 file changed

+60
-65
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+60
-65
lines changed

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

Lines changed: 60 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -35,78 +35,73 @@ Design a data structure that supports all following operations in average O(1) t
3535
// getRandom should return 1 and 2 both equally likely.
3636
collection.getRandom();*/
3737
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();
6354
}
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-
}
6955

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;
8164
} else {
82-
//remove the entry in revserve map that has val as its value
83-
reverseMap.values().remove(val);
65+
contains = false;
8466
}
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;
8772
}
88-
return contains;
89-
}
9073

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;
9894
}
99-
return reverseMap.get(randNum);
100-
}
10195

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+
}
110106
}
111-
112107
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy