We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 3c13908 + e2e8cbd commit 5e52ea6Copy full SHA for 5e52ea6
swift/0380-Insert-Delete-GetRandom-O(1).Swift
@@ -0,0 +1,37 @@
1
+class RandomizedSet {
2
+ var random = Set<Int>()
3
+
4
+ init() {
5
6
+ }
7
8
+ func insert(_ val: Int) -> Bool {
9
+ if !random.contains(val){
10
+ random.insert(val)
11
+ return true
12
+ } else {
13
+ return false
14
15
16
17
+ func remove(_ val: Int) -> Bool {
18
+ if random.contains(val) {
19
+ random.remove(val)
20
21
22
23
24
25
26
+ func getRandom() -> Int {
27
+ return random.randomElement()!
28
29
+}
30
31
+/**
32
+ * Your RandomizedSet object will be instantiated and called as such:
33
+ * let obj = RandomizedSet()
34
+ * let ret_1: Bool = obj.insert(val)
35
+ * let ret_2: Bool = obj.remove(val)
36
+ * let ret_3: Int = obj.getRandom()
37
+ */
0 commit comments