class InsertDeleteGetRandom {
/** Initialize your data structure here. */
private val set = mutableSetOf<Int>()
private val rand = Random(System.currentTimeMillis())
/** Inserts a value to the set. Returns trueif the set did not already contain the specified element. */
fun insert(`val`: Int): Boolean {
return set.add(`val`)
}
/** Removes a value from the set. Returns trueif the set contained the specified element. */
fun remove(`val`: Int): Boolean {
return set.remove(`val`)
}
/** Get a random element from the set. */
fun getRandom(): Int {
val index = rand.nextInt(0, set.size)
return set.elementAt(index)
}
}
複製代碼