/
380_Insert_Delete_GetRandom_O(1).swift
50 lines (42 loc) · 1.4 KB
/
380_Insert_Delete_GetRandom_O(1).swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Foundation
class RandomizedSet {
var valueIdxMap = [Int:Int]()
var valueList = [Int]()
/** Initialize your data structure here. */
init() {
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
func insert(_ val: Int) -> Bool {
guard self.valueIdxMap[val] == nil else {
return false
}
self.valueIdxMap[val] = self.valueList.count
self.valueList.append(val)
return true
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
func remove(_ val: Int) -> Bool {
guard self.valueIdxMap[val] != nil else {
return false
}
let idx = self.valueIdxMap[val]
let lastVal = self.valueList.last
self.valueList[idx!] = lastVal!
self.valueIdxMap[lastVal!] = idx
self.valueList.popLast()
self.valueIdxMap.removeValue(forKey: val)
return true
}
/** Get a random element from the set. */
func getRandom() -> Int {
var idx = Int.random(in: 0..<self.valueList.count)
return self.valueList[idx]
}
}
/**
* Your RandomizedSet object will be instantiated and called as such:
* let obj = RandomizedSet()
* let ret_1: Bool = obj.insert(val)
* let ret_2: Bool = obj.remove(val)
* let ret_3: Int = obj.getRandom()
*/