class RandomizedSet {
public:
/** Initialize your data structure here. */
RandomizedSet() {
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
bool insert(int val) {
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
bool remove(int val) {
}
/** Get a random element from the set. */
int getRandom() {
}
};
/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet* obj = new RandomizedSet();
* bool param_1 = obj->insert(val);
* bool param_2 = obj->remove(val);
* int param_3 = obj->getRandom();
*/
class RandomizedSet:
def __init__(self):
"""
Initialize your data structure here.
"""
self.nums = []
def insert(self, val):
"""
Inserts a value to the set. Returns true if the set did not already contain the specified element.
:type val: int
:rtype: bool
"""
if val not in self.nums:
self.nums.append(val)
return True
return False
def remove(self, val):
"""
Removes a value from the set. Returns true if the set contained the specified element.
:type val: int
:rtype: bool
"""
if val in self.nums:
self.nums.remove(val)
return True
return False
def getRandom(self):
"""
Get a random element from the set.
:rtype: int
"""
from random import randint
k = randint(0, len(self.nums)-1)
return self.nums[k]
# Your RandomizedSet object will be instantiated and called as such:
# obj = RandomizedSet()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()