Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit99f729a

Browse files
RandomizedSet
1 parent109be8d commit99f729a

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

‎MEDIUM/src/medium/RandomizedSet.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
packagemedium;
2+
3+
importjava.util.HashMap;
4+
importjava.util.HashSet;
5+
importjava.util.Iterator;
6+
importjava.util.Map;
7+
importjava.util.Random;
8+
importjava.util.Set;
9+
10+
/**This solution get AC'ed. Although it's not really doing random*/
11+
publicclassRandomizedSet {
12+
13+
Set<Integer>set;
14+
15+
/** Initialize your data structure here. */
16+
publicRandomizedSet() {
17+
set =newHashSet();
18+
}
19+
20+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
21+
publicbooleaninsert(intval) {
22+
if(set.contains(val))returnfalse;
23+
else {
24+
set.add(val);
25+
returntrue;
26+
}
27+
}
28+
29+
/** Deletes a value from the set. Returns true if the set contained the specified element. */
30+
publicbooleandelete(intval) {
31+
returnset.remove(val);
32+
}
33+
34+
/** Get a random element from the set. */
35+
publicintgetRandom() {
36+
Iterator<Integer>it =set.iterator();
37+
if(!it.hasNext())return -1;
38+
intres =0;
39+
while(it.hasNext()){
40+
res =it.next();
41+
break;
42+
}
43+
returnres;
44+
}
45+
46+
publicstaticvoidmain(String...args){
47+
RandomizedSet_2nd_solutiontest =newRandomizedSet_2nd_solution();
48+
System.out.println(test.insert(1));
49+
System.out.println(test.delete(2));
50+
System.out.println(test.insert(2));
51+
System.out.println(test.getRandom());
52+
System.out.println(test.delete(1));
53+
System.out.println(test.insert(2));
54+
System.out.println(test.getRandom());
55+
}
56+
}
57+
58+
/**
59+
* Your RandomizedSet object will be instantiated and called as such:
60+
* RandomizedSet obj = new RandomizedSet();
61+
* boolean param_1 = obj.insert(val);
62+
* boolean param_2 = obj.delete(val);
63+
* int param_3 = obj.getRandom();
64+
*/
65+
66+
/**TODO: submit this solution on OJ later, it's throwing cannot find symbol remove() method on line 16,
67+
* this is an OJ bug that people reported on Discuss, submit it later, see if it can get AC'ed.*/
68+
classRandomizedSet_2nd_solution {
69+
70+
Map<Integer,Integer>forwardMap;//key is auto increment index, value if the inserted val
71+
Map<Integer,Integer>reverseMap;//the other way around
72+
intindex;
73+
Randomrandom;
74+
75+
/** Initialize your data structure here. */
76+
publicRandomizedSet_2nd_solution() {
77+
forwardMap =newHashMap();
78+
reverseMap =newHashMap();
79+
index =0;
80+
random =newRandom();
81+
}
82+
83+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
84+
publicbooleaninsert(intval) {
85+
if(forwardMap.containsValue(val))returnfalse;
86+
else {
87+
forwardMap.put(index,val);
88+
reverseMap.put(val,index++);
89+
returntrue;
90+
}
91+
}
92+
93+
/** Deletes a value from the set. Returns true if the set contained the specified element. */
94+
publicbooleandelete(intval) {
95+
if(forwardMap.containsValue(val)){
96+
intkey =reverseMap.get(val);
97+
reverseMap.remove(val);
98+
forwardMap.remove(key);
99+
returntrue;
100+
}else {
101+
returnfalse;
102+
}
103+
}
104+
105+
/** Get a random element from the set. */
106+
publicintgetRandom() {
107+
intmax =forwardMap.size();
108+
intrandomNum =random.nextInt(max) ==0 ?1 :random.nextInt(max);
109+
returnforwardMap.get(randomNum);
110+
}
111+
112+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp