LeetCode-208.Implement Trie(Prefix Tree)
阿新 • • 發佈:2019-04-02
eth child ali public clas leetcode ini ring ==
Implement a trie with insert
, search
, and startsWith
methods.
Example:
Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // returns true trie.search("app"); // returns false trie.startsWith("app"); // returns true trie.insert("app"); trie.search("app"); // returns true
Note:
- You may assume that all inputs are consist of lowercase letters
a-z
. - All inputs are guaranteed to be non-empty strings.
使用數組保存子節點,但是不可擴展,效率高
使用ArrayList保存子節點,可擴展,但查找效率差
使用Map保存子節點
1 class Trie {// mytip 2 TrieNode root ; 3 4 /** Initialize your data structure here. */5 public Trie() { 6 root= new TrieNode(); 7 } 8 9 /** Inserts a word into the trie. */ 10 public void insert(String word) { 11 TrieNode cur = root; 12 if(null==cur){ 13 cur = new TrieNode(); 14 } 15 for (int i = 0; i < word.length(); i++) {16 char c = word.charAt(i); 17 List<TrieNode> list = cur.children; 18 boolean flag = false; 19 for (int j = 0; j < list.size() ; j++) { 20 if(list.get(j).val==c){ 21 cur = list.get(j); 22 flag = true; 23 break; 24 } 25 } 26 if(!flag){ 27 TrieNode t = new TrieNode(c); 28 cur.children.add(t); 29 cur = t; 30 } 31 } 32 cur.isWord = true; 33 } 34 35 /** Returns if the word is in the trie. */ 36 public boolean search(String word) { 37 TrieNode cur = root; 38 for (int i = 0; i < word.length(); i++) { 39 char c = word.charAt(i); 40 if(null==cur||null==cur.children||0==cur.children.size()){ 41 return false; 42 } 43 List<TrieNode> list = cur.children; 44 boolean flag = false; 45 for (int j = 0; j < list.size() ; j++) { 46 if(list.get(j).val==c){ 47 cur = list.get(j); 48 flag =true; 49 break; 50 } 51 } 52 if(!flag){ 53 return false; 54 } 55 } 56 return cur.isWord; 57 } 58 59 /** Returns if there is any word in the trie that starts with the given prefix. */ 60 public boolean startsWith(String prefix) { 61 TrieNode cur = root; 62 for (int i = 0; i < prefix.length(); i++) { 63 char c = prefix.charAt(i); 64 if(null==cur||null==cur.children||0==cur.children.size()){ 65 return false; 66 } 67 List<TrieNode> list = cur.children; 68 boolean flag = false; 69 for (int j = 0; j < list.size() ; j++) { 70 if(list.get(j).val==c){ 71 cur = list.get(j); 72 flag =true; 73 break; 74 } 75 } 76 if(!flag){ 77 return false; 78 } 79 } 80 return true; 81 } 82 } 83 84 class TrieNode{ 85 List<TrieNode> children;//孩子結點使用List存放 效率較低 86 char val; 87 boolean isWord; 88 TrieNode(){ 89 children = new ArrayList<>(); 90 isWord = false; 91 } 92 TrieNode(char c){ 93 children = new ArrayList<>(); 94 isWord = false; 95 val =c; 96 } 97 }
1 class Trie {//my 2 TrieNode root ; 3 4 /** Initialize your data structure here. */ 5 public Trie() { 6 root= new TrieNode(); 7 } 8 9 /** Inserts a word into the trie. */ 10 public void insert(String word) { 11 TrieNode cur = root; 12 if(null==cur){ 13 cur = new TrieNode(); 14 } 15 for (int i = 0; i < word.length(); i++) { 16 char c = word.charAt(i); 17 Map<Character,TrieNode> list = cur.children; 18 if(list.containsKey(c)){ 19 cur = list.get(c); 20 } 21 else{ 22 TrieNode t = new TrieNode(c); 23 cur.children.put(c,t); 24 cur = t; 25 } 26 } 27 cur.isWord = true; 28 } 29 30 /** Returns if the word is in the trie. */ 31 public boolean search(String word) { 32 TrieNode cur = root; 33 for (int i = 0; i < word.length(); i++) { 34 char c = word.charAt(i); 35 if(null==cur||null==cur.children||0==cur.children.size()){ 36 return false; 37 } 38 Map<Character,TrieNode> list = cur.children; 39 if(list.containsKey(c)){ 40 cur = list.get(c); 41 } 42 else{ 43 return false; 44 } 45 } 46 return cur.isWord; 47 } 48 49 /** Returns if there is any word in the trie that starts with the given prefix. */ 50 public boolean startsWith(String prefix) { 51 TrieNode cur = root; 52 for (int i = 0; i < prefix.length(); i++) { 53 char c = prefix.charAt(i); 54 if(null==cur||null==cur.children||0==cur.children.size()){ 55 return false; 56 } 57 Map<Character,TrieNode> list = cur.children; 58 if(list.containsKey(c)){ 59 cur = list.get(c); 60 } 61 else{ 62 return false; 63 } 64 } 65 return true; 66 } 67 } 68 69 class TrieNode{ 70 Map<Character,TrieNode> children;//使用map存放子節點 71 char val; 72 boolean isWord; 73 TrieNode(){ 74 children = new HashMap<>(); 75 isWord = false; 76 } 77 TrieNode(char c){ 78 children = new HashMap<>(); 79 isWord = false; 80 val =c; 81 } 82 }
LeetCode-208.Implement Trie(Prefix Tree)