Movatterモバイル変換


[0]ホーム

URL:


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

Commit96aaa13

Browse files
implement trie
1 parentf44ceca commit96aaa13

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

‎MEDIUM/src/medium/ImplementTrie.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
packagemedium;
2+
3+
publicclassImplementTrie {
4+
classTrieNode {
5+
6+
charval;
7+
booleanisWord;
8+
TrieNode[]children =newTrieNode[26];
9+
10+
// Initialize your data structure here.
11+
publicTrieNode() {}
12+
13+
publicTrieNode(charc) {
14+
this.val =c;
15+
}
16+
}
17+
18+
publicclassTrie {
19+
privateTrieNoderoot;
20+
21+
publicTrie() {
22+
root =newTrieNode();
23+
root.val =' ';//initialize root to be an empty char, this is a common practice as how Wiki defines Trie data structure as well
24+
}
25+
26+
// Inserts a word into the trie.
27+
publicvoidinsert(Stringword) {
28+
TrieNodenode =root;
29+
for(inti =0;i <word.length();i++){
30+
if(node.children[word.charAt(i) -'a'] ==null){
31+
node.children[word.charAt(i) -'a'] =newTrieNode(word.charAt(i));
32+
}
33+
node =node.children[word.charAt(i) -'a'];
34+
}
35+
node.isWord =true;
36+
}
37+
38+
// Returns if the word is in the trie.
39+
publicbooleansearch(Stringword) {
40+
TrieNodenode =root;
41+
for(inti =0;i <word.length();i++){
42+
if(node.children[word.charAt(i) -'a'] ==null)returnfalse;
43+
node =node.children[word.charAt(i) -'a'];
44+
}
45+
returnnode.isWord;
46+
}
47+
48+
// Returns if there is any word in the trie
49+
// that starts with the given prefix.
50+
publicbooleanstartsWith(Stringprefix) {
51+
TrieNodenode =root;
52+
for(inti =0;i <prefix.length();i++){
53+
if(node.children[prefix.charAt(i) -'a'] ==null)returnfalse;
54+
node =node.children[prefix.charAt(i) -'a'];
55+
}
56+
returntrue;
57+
}
58+
}
59+
60+
// Your Trie object will be instantiated and called as such:
61+
// Trie trie = new Trie();
62+
// trie.insert("somestring");
63+
// trie.search("key");
64+
}

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
|441|[Arranging Coins](https://leetcode.com/problems/arrange-coins/)|[Solution](../../blob/master/EASY/src/easy/ArrangingCoins.java)| O(n)|O(1)| Easy|
77
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval/)|[Solution](../../blob/master/MEDIUM/src/medium/FindRightInterval.java) | O(nlogn) |O(n) | Medium| Binary Search
88
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../../blob/master/MEDIUM/src/medium/NonOverlappingIntervals.java) | O(nlogn) |O(1) | Medium| Greedy
9+
|420|[Strong Password Checker](https://leetcode.com/problems/strong-password-checker/)|[Solution](../../blob/master/HARD/src/hard/StrongPasswordChecker.java)| ?| ?| Hard|
910
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/)|[Solution](../../blob/master/MEDIUM/src/medium/BattleshipsinaBoard.java) | O(n^2) |O(1) | Medium| DFS
1011
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)|[Solution](../../blob/master/MEDIUM/src/medium/PacificAtlanticWaterFlow.java) | O(m*n*Max(m,n)) |O(m*n) | Medium| DFS
1112
|415|[Add Strings](https://leetcode.com/problems/add-strings/)|[Solution](../../blob/master/EASY/src/easy/AddStrings.java)| O(n)|O(1)| Easy|
@@ -39,6 +40,7 @@
3940
|223|[Rectangle Area](https://leetcode.com/problems/rectangle-area/)|[Solution](../../blob/master/EASY/src/easy/RectangleArea.java)| O(1)|O(1)| Easy|
4041
|219|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)|[Solution](../../blob/master/EASY/src/easy/ContainsDuplicateII.java)| O(n)|O(n) | Easy| HashMap
4142
|209|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)|[Solution](../../blob/master/MEDIUM/src/medium/MinimumSizeSubarraySum.java)| O(n)|O(1)| Medium|
43+
|208|[Implement Trie](https://leetcode.com/problems/implement-trie-prefix-tree/)|[Solution](../../blob/master/MEDIUM/src/medium/ImplementTrie.java)| O(n)|O(1)| Medium|
4244
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)|[Solution](../../blob/master/EASY/src/easy/ReverseLinkedList.java)| O(n)|O(1) | Easy
4345
|205|[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)|[Solution](../../blob/master/EASY/src/easy/IsomorphicStrings.java)| O(n)|O(1) | Easy
4446
|200|[Number of Islands](https://leetcode.com/problems/number-of-islands/)|[Union Find](../../blob/master/MEDIUM/src/medium/NumberOfIslandsUnionFind.java)[DFS](../../blob/master/MEDIUM/src/medium/NumberofIslandsDFS.java)| O(m*n)|O(m*n) | Medium| Union Find, DFS

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp