Skip to content

Commit 2fcc5b5

Browse files
committed
再次完成380
1 parent 66e20f2 commit 2fcc5b5

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

docs/0380-insert-delete-getrandom-o1.adoc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,21 @@ randomSet.insert(2);
4141
randomSet.getRandom();
4242
----
4343

44-
44+
== 思路分析
4545

4646
[[src-0380]]
4747
[{java_src_attr}]
4848
----
4949
include::{sourcedir}/_0380_InsertDeleteGetRandomO1.java[]
5050
----
5151

52+
[{java_src_attr}]
53+
----
54+
include::{sourcedir}/_0380_InsertDeleteGetRandomO1_2.java[]
55+
----
56+
57+
== 参考资料
58+
59+
. https://leetcode.cn/problems/insert-delete-getrandom-o1/solutions/1411578/o1-shi-jian-cha-ru-shan-chu-he-huo-qu-su-rlz2/[380. O(1) 时间插入、删除和获取随机元素 - 官方题解^]
60+
. https://leetcode.cn/problems/insert-delete-getrandom-o1/solutions/1416888/by-ac_oier-tpex/[380. O(1) 时间插入、删除和获取随机元素 - 【宫水三叶】数据结构运用题^]
61+

logbook/202406.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,15 @@
234234
|{doc_base_url}/1094-car-pooling.adoc[题解]
235235
|差分数组
236236

237-
238237
|1109
239238
|{leetcode_base_url}/corporate-flight-bookings/[Corporate Flight Bookings]
240239
|{doc_base_url}/1109-corporate-flight-bookings.adoc[题解]
241240
|差分数组
242241

242+
|380
243+
|{leetcode_base_url}/insert-delete-getrandom-o1/[Insert Delete GetRandom O(1)]
244+
|{doc_base_url}/0380-insert-delete-getrandom-o1.adoc[题解]
245+
|数据结构应用
246+
243247

244248
|===
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import java.util.*;
4+
5+
/**
6+
* = 380. Insert Delete GetRandom O(1)
7+
*
8+
* https://leetcode.com/problems/insert-delete-getrandom-o1/[Insert Delete GetRandom O(1) - LeetCode]
9+
*
10+
* Design a data structure that supports all following operations in average *O(1)* time.
11+
*
12+
* . `insert(val)`: Inserts an item val to the set if not already present.
13+
* . `remove(val)`: Removes an item val from the set if present.
14+
* . `getRandom`: Returns a random element from current set of elements. Each element must have the *same probability* of being returned.
15+
*
16+
* .Example:
17+
* [source]
18+
* ----
19+
* // Init an empty set.
20+
* RandomizedSet randomSet = new RandomizedSet();
21+
*
22+
* // Inserts 1 to the set. Returns true as 1 was inserted successfully.
23+
* randomSet.insert(1);
24+
*
25+
* // Returns false as 2 does not exist in the set.
26+
* randomSet.remove(2);
27+
*
28+
* // Inserts 2 to the set, returns true. Set now contains [1,2].
29+
* randomSet.insert(2);
30+
*
31+
* // getRandom should return either 1 or 2 randomly.
32+
* randomSet.getRandom();
33+
*
34+
* // Removes 1 from the set, returns true. Set now contains [2].
35+
* randomSet.remove(1);
36+
*
37+
* // 2 was already in the set, so return false.
38+
* randomSet.insert(2);
39+
*
40+
* // Since 2 is the only number in the set, getRandom always return 2.
41+
* randomSet.getRandom();
42+
* ----
43+
*
44+
* @author D瓜哥 · https://www.diguage.com
45+
* @since 2024-07-05 16:44:29
46+
*/
47+
public class _0380_InsertDeleteGetRandomO1_2 {
48+
/**
49+
* 自己实现
50+
*/
51+
class RandomizedSet {
52+
private List<Integer> nums;
53+
private Map<Integer, Integer> idex;
54+
private Random random;
55+
56+
public RandomizedSet() {
57+
nums = new ArrayList<>();
58+
random = new Random();
59+
idex = new HashMap<>();
60+
}
61+
62+
public boolean insert(int val) {
63+
if (idex.containsKey(val)) {
64+
return false;
65+
} else {
66+
nums.add(val);
67+
idex.put(val, nums.size() - 1);
68+
return true;
69+
}
70+
}
71+
72+
public boolean remove(int val) {
73+
if (!idex.containsKey(val)) {
74+
return false;
75+
} else {
76+
int i1 = idex.get(val);
77+
if (i1 != idex.size() - 1) {
78+
Integer value = nums.get(nums.size() - 1);
79+
nums.set(i1, value);
80+
idex.put(value, i1);
81+
}
82+
idex.remove(val);
83+
nums.remove(nums.size() - 1);
84+
return true;
85+
}
86+
}
87+
88+
public int getRandom() {
89+
return nums.get(random.nextInt(nums.size()));
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)