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 0b23246 + dccae42 commit d182fc2Copy full SHA for d182fc2
contest/src/main/java/com/github/contest/design/DesignLeetcode.kt
@@ -152,4 +152,32 @@ class TimeMap() {
152
153
private fun emptyString() = ""
154
155
+}
156
+
157
+/**
158
+ * 380. Insert Delete GetRandom O(1)
159
+ */
160
161
+class RandomizedSet() {
162
163
+ private val store = mutableMapOf<Int, Int>()
164
165
+ fun insert(`val`: Int): Boolean = when {
166
+ store.contains(`val`) -> false
167
+ else -> {
168
+ store[`val`] = store.getOrDefault(`val`, 0) + 1
169
+ true
170
+ }
171
172
173
+ fun remove(`val`: Int): Boolean = when {
174
+ !store.contains(`val`) -> false
175
176
+ store.remove(`val`)
177
178
179
180
181
+ fun getRandom(): Int = store.keys.random()
182
183
}
0 commit comments