Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1500172

Browse files
Chapter 10 Trie Optional 02 Trie delete codes added.
1 parent 6d68bcd commit 1500172

File tree

6 files changed

+690
-1
lines changed

6 files changed

+690
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/// Leetcode 380. Insert Delete GetRandom O(1)
2+
/// https://leetcode.com/problems/insert-delete-getrandom-o1/description/
3+
///
4+
/// 使用380号问题测试我们的代码
5+
/// 该版本代码使用Java内置的HashMap,作为算法实现的基准
6+
7+
import java.util.Random;
8+
import java.util.HashMap;
9+
import java.util.ArrayList;
10+
11+
12+
public class RandomizedSet_HashMap {
13+
14+
HashMap<String, Integer> map;
15+
ArrayList<Integer> nums;
16+
17+
/** Initialize your data structure here. */
18+
public RandomizedSet_HashMap() {
19+
map = new HashMap();
20+
nums = new ArrayList<>();
21+
}
22+
23+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
24+
public boolean insert(int val) {
25+
26+
String key = Integer.toString(val);
27+
if(map.containsKey(key))
28+
return false;
29+
30+
nums.add(val);
31+
int index = nums.size() - 1;
32+
map.put(key, index);
33+
return true;
34+
}
35+
36+
/** Removes a value from the set. Returns true if the set contained the specified element. */
37+
public boolean remove(int val) {
38+
39+
String key = Integer.toString(val);
40+
if(!map.containsKey(key))
41+
return false;
42+
43+
int index = map.get(key);
44+
map.remove(key);
45+
46+
int num = nums.get(nums.size() - 1);
47+
nums.remove(nums.size() - 1);
48+
49+
if(num != val) {
50+
nums.set(index, num);
51+
map.put(Integer.toString(num), index);
52+
}
53+
54+
return true;
55+
}
56+
57+
/** Get a random element from the set. */
58+
public int getRandom() {
59+
60+
Random random = new Random();
61+
int index = random.nextInt(nums.size());
62+
return nums.get(index);
63+
}
64+
65+
public static void main(String[] args){
66+
67+
RandomizedSet_TrieR rs = new RandomizedSet_TrieR();
68+
rs.insert(0);
69+
rs.remove(0);
70+
rs.insert(-1);
71+
rs.remove(0);
72+
}
73+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/// Leetcode 380. Insert Delete GetRandom O(1)
2+
/// https://leetcode.com/problems/insert-delete-getrandom-o1/description/
3+
///
4+
/// 使用380号问题测试我们的代码
5+
/// 该版本代码用于测试非递归实现的TrieMap
6+
7+
import java.util.Random;
8+
import java.util.Stack;
9+
import java.util.TreeMap;
10+
import java.util.ArrayList;
11+
import java.util.Random;
12+
13+
public class RandomizedSet_Trie {
14+
15+
private class TrieMap {
16+
17+
private class Node{
18+
19+
public boolean isWord;
20+
public TreeMap<Character, Node> next;
21+
public int val;
22+
23+
public Node(boolean isWord, int val){
24+
this.isWord = isWord;
25+
next = new TreeMap<>();
26+
this.val = val;
27+
}
28+
29+
public Node(){
30+
this(false, -1);
31+
}
32+
}
33+
34+
private Node root;
35+
private int size;
36+
37+
public TrieMap(){
38+
root = new Node();
39+
size = 0;
40+
}
41+
42+
// 获得Trie中存储的单词数量
43+
public int getSize(){
44+
return size;
45+
}
46+
47+
// 向Trie中添加一个新的单词word
48+
public void add(String word, int val){
49+
50+
Node cur = root;
51+
for(int i = 0 ; i < word.length() ; i ++){
52+
char c = word.charAt(i);
53+
if(cur.next.get(c) == null)
54+
cur.next.put(c, new Node());
55+
cur = cur.next.get(c);
56+
}
57+
58+
if(!cur.isWord){
59+
cur.isWord = true;
60+
size ++;
61+
}
62+
cur.val = val;
63+
}
64+
65+
// 查询单词word是否在Trie中
66+
public boolean contains(String word){
67+
68+
Node cur = root;
69+
for(int i = 0 ; i < word.length() ; i ++){
70+
char c = word.charAt(i);
71+
if(cur.next.get(c) == null)
72+
return false;
73+
cur = cur.next.get(c);
74+
}
75+
return cur.isWord;
76+
}
77+
78+
// 查询单词word所对应的值
79+
public int get(String word){
80+
81+
Node cur = root;
82+
for(int i = 0 ; i < word.length() ; i ++){
83+
char c = word.charAt(i);
84+
if(cur.next.get(c) == null)
85+
throw new RuntimeException("Can not get");
86+
cur = cur.next.get(c);
87+
}
88+
return cur.val;
89+
}
90+
91+
// 删除word, 返回是否删除成功
92+
public boolean remove(String word){
93+
94+
Stack<Node> stack = new Stack<>();
95+
stack.push(root);
96+
for(int i = 0; i < word.length(); i ++){
97+
if(!stack.peek().next.containsKey(word.charAt(i)))
98+
return false;
99+
stack.push(stack.peek().next.get(word.charAt(i)));
100+
}
101+
102+
if(!stack.peek().isWord)
103+
return false;
104+
105+
// 将该单词结尾isWord置空
106+
stack.peek().isWord = false;
107+
size --;
108+
109+
// 如果单词最后一个字母的节点的next非空,
110+
// 说明trie中还存储了其他以该单词为前缀的单词,直接返回
111+
if(stack.peek().next.size() > 0)
112+
return true;
113+
else
114+
stack.pop();
115+
116+
// 自底向上删除
117+
for(int i = word.length() - 1; i >= 0; i --){
118+
stack.peek().next.remove(word.charAt(i));
119+
// 如果上一个节点的isWord为true,或者是其他单词的前缀,则直接返回
120+
if(stack.peek().isWord || stack.peek().next.size() > 0)
121+
return true;
122+
}
123+
return true;
124+
}
125+
}
126+
127+
TrieMap map;
128+
ArrayList<Integer> nums;
129+
130+
/** Initialize your data structure here. */
131+
public RandomizedSet_Trie() {
132+
map = new TrieMap();
133+
nums = new ArrayList<>();
134+
}
135+
136+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
137+
public boolean insert(int val) {
138+
139+
String key = Integer.toString(val);
140+
if(map.contains(key))
141+
return false;
142+
143+
nums.add(val);
144+
int index = nums.size() - 1;
145+
map.add(key, index);
146+
return true;
147+
}
148+
149+
/** Removes a value from the set. Returns true if the set contained the specified element. */
150+
public boolean remove(int val) {
151+
152+
String key = Integer.toString(val);
153+
if(!map.contains(key))
154+
return false;
155+
156+
int index = map.get(key);
157+
map.remove(key);
158+
159+
int num = nums.get(nums.size() - 1);
160+
nums.remove(nums.size() - 1);
161+
162+
if(num != val) {
163+
nums.set(index, num);
164+
map.add(Integer.toString(num), index);
165+
}
166+
167+
return true;
168+
}
169+
170+
/** Get a random element from the set. */
171+
public int getRandom() {
172+
173+
Random random = new Random();
174+
int index = random.nextInt(nums.size());
175+
return nums.get(index);
176+
}
177+
178+
public static void main(String[] args){
179+
180+
RandomizedSet_TrieR rs = new RandomizedSet_TrieR();
181+
rs.insert(0);
182+
rs.remove(0);
183+
rs.insert(-1);
184+
rs.remove(0);
185+
}
186+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /