日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

【LeetCode笔记】208. 实现Trie(前缀树)(Java、前缀树)

發(fā)布時間:2024/7/23 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LeetCode笔记】208. 实现Trie(前缀树)(Java、前缀树) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 題目描述
  • 思路 & 代碼
      • 更新版

題目描述

  • 屬于那種,敲過一遍就不會忘了的那種題,就是學一個新的數(shù)據(jù)結(jié)構= =
  • 二十六叉樹!

思路 & 代碼

  • isEnd 非常重要噢,只有正式 insert 了的單詞才能被 search 到。
  • 在 Trie 中實現(xiàn)內(nèi)部類 TrieNode
  • 說實話,insert、search、startWith 三個函數(shù)的結(jié)構都十分相似= =
  • 注意 new Node 的時候小心。。引用丟失就難受了
class Trie {private class TrieNode{boolean isEnd;TrieNode[] next;TrieNode(){isEnd = false;next = new TrieNode[26];}}private TrieNode root;/** Initialize your data structure here. */public Trie() {root = new TrieNode();}/** Inserts a word into the trie. */// 沒則插,結(jié)尾改 true (出現(xiàn)了,但是沒真正 insert 的話,不算:比如 insert apple , 然后 search app = falsepublic void insert(String word) {char[] wordC = word.toCharArray();TrieNode nowNode = root;for(int i = 0; i < wordC.length; i++){// error:TrieNode nextNode = nowNode.next[wordC[i] - 'a']; nextNode = new TrieNode()// 會直接丟失引用!int index = wordC[i] - 'a';if(nowNode.next[index] == null){nowNode.next[index] = new TrieNode();}nowNode = nowNode.next[index];}// 結(jié)束,標志單詞結(jié)尾字符nowNode.isEnd = true;}/** Returns if the word is in the trie. */public boolean search(String word) {char[] wordC = word.toCharArray();TrieNode nowNode = root;for(int i = 0; i < wordC.length; i++){int index = wordC[i] - 'a';if(nowNode.next[index] == null){return false;}nowNode = nowNode.next[index];}return nowNode.isEnd;}/** Returns if there is any word in the trie that starts with the given prefix. */public boolean startsWith(String prefix) {char[] wordC = prefix.toCharArray();TrieNode nowNode = root;for(int i = 0; i < wordC.length; i++){int index = wordC[i] - 'a';if(nowNode.next[index] == null){return false;}nowNode = nowNode.next[index];}return true;} }/*** Your Trie object will be instantiated and called as such:* Trie obj = new Trie();* obj.insert(word);* boolean param_2 = obj.search(word);* boolean param_3 = obj.startsWith(prefix);*/

更新版

  • 其實三個函數(shù)的代碼都差不多= =
  • 前綴樹:存儲字符串的一種樹結(jié)構(二十六叉樹),可以實現(xiàn)拼寫檢查、自動補全功能。
class Trie {TrieNode root;private class TrieNode {boolean isEnd;TrieNode[] nextNode;public TrieNode() {isEnd = false;nextNode = new TrieNode[26];}}public Trie() {root = new TrieNode();}public void insert(String word) {TrieNode nowNode = root;for(char c : word.toCharArray()) {int index = c - 'a';if(nowNode.nextNode[index] == null) {nowNode.nextNode[index] = new TrieNode();}nowNode = nowNode.nextNode[index];}nowNode.isEnd = true;}public boolean search(String word) {TrieNode nowNode = root;for(char c : word.toCharArray()) {int index = c - 'a';if(nowNode.nextNode[index] == null) {return false;}nowNode = nowNode.nextNode[index];}return nowNode.isEnd;}public boolean startsWith(String prefix) {TrieNode nowNode = root;for(char c : prefix.toCharArray()) {int index = c - 'a';if(nowNode.nextNode[index] == null) {return false;}nowNode = nowNode.nextNode[index];}return true;} }

總結(jié)

以上是生活随笔為你收集整理的【LeetCode笔记】208. 实现Trie(前缀树)(Java、前缀树)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。