Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ade79b1

Browse files
refactor 380
1 parent cd295c0 commit ade79b1

File tree

1 file changed

+55
-51
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+55
-51
lines changed

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

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -46,64 +46,68 @@ public class _380 {
4646
* boolean param_2 = obj.delete(val);
4747
* int param_3 = obj.getRandom();
4848
*/
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 {
5152

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;
5658

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();
7767
}
78-
}
7968

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+
}
9181
}
92-
}
9382

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+
}
10195
}
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);
105110
}
106-
return forwardMap.get(randomNum);
107111
}
108112
}
109-
}
113+
}

0 commit comments

Comments
 (0)