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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

leetcode 208. Implement Trie (Prefix Tree) | 208. 实现 Trie 前缀树(Java)

發布時間:2024/2/28 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 208. Implement Trie (Prefix Tree) | 208. 实现 Trie 前缀树(Java) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

https://leetcode.com/problems/implement-trie-prefix-tree/

題解

第一版:暴力法

import java.util.LinkedHashSet;class Trie {LinkedHashSet<String> set;/** Initialize your data structure here. */public Trie() {set = new LinkedHashSet<>();}/** Inserts a word into the trie. */public void insert(String word) {set.add(word);}/** Returns if the word is in the trie. */public boolean search(String word) {return set.contains(word);}/** Returns if there is any word in the trie that starts with the given prefix. */public boolean startsWith(String prefix) {for (String s : set)if (s.startsWith(prefix)) return true;return false;} }/*** 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);*/

第二版:前綴樹的實現

參考:官方題解

Trie node structure

Insertion of a key to a trie

Search for a key in a trie


Search for a key prefix in a trie

My Code
class Node{Node[] map;boolean isEnd;public Node() {this.map = new Node['z' - 'a' + 1];}public Node get(char c) {return map[c - 'a'];}public Node put(char c) {Node node = new Node();map[c - 'a'] = node;return node;} } class Trie {Node node;/** Initialize your data structure here. */public Trie() {node=new Node();}/** Inserts a word into the trie. */public void insert(String word) {char[] chars = word.toCharArray();Node cur = node;for (int i = 0; i < chars.length; i++) {if (cur.get(chars[i]) != null)cur = cur.get(chars[i]);elsecur = cur.put(chars[i]);if (i == chars.length - 1)cur.isEnd = true;}}/** Returns if the word is in the trie. */public boolean search(String word) {Node node = searchPrefix(word);return node != null && node.isEnd;}/** Returns if there is any word in the trie that starts with the given prefix. */public boolean startsWith(String prefix) {Node node = searchPrefix(prefix);return node != null;}public Node searchPrefix(String prefix) {char[] chars = prefix.toCharArray();Node cur = node;for (char c : chars) {if (cur.get(c) == null) return null;// cur.isEnd可能是偽結束符,不能用來判斷是否真正結束遍歷else cur = cur.get(c);}return cur;} }/*** 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);*/


附:左神算法 - 子數組最大異或和





package chapter_5_stringproblem;public class Problem_24_TrieTree {public static class TrieNode {public int path;public int end;public TrieNode[] map;public TrieNode() {path = 0;end = 0;map = new TrieNode[26];}}public static class Trie {private TrieNode root;public Trie() {root = new TrieNode();}public void insert(String word) {if (word == null) {return;}char[] chs = word.toCharArray();TrieNode node = root;node.path++;int index = 0;for (int i = 0; i < chs.length; i++) {index = chs[i] - 'a';if (node.map[index] == null) {node.map[index] = new TrieNode();}node = node.map[index];node.path++;}node.end++;}public void delete(String word) {if (search(word)) {char[] chs = word.toCharArray();TrieNode node = root;node.path--;int index = 0;for (int i = 0; i < chs.length; i++) {index = chs[i] - 'a';if (node.map[index].path-- == 1) {node.map[index] = null;return;}node = node.map[index];}node.end--;}}public boolean search(String word) {if (word == null) {return false;}char[] chs = word.toCharArray();TrieNode node = root;int index = 0;for (int i = 0; i < chs.length; i++) {index = chs[i] - 'a';if (node.map[index] == null) {return false;}node = node.map[index];}return node.end != 0;}public int prefixNumber(String pre) {if (pre == null) {return 0;}char[] chs = pre.toCharArray();TrieNode node = root;int index = 0;for (int i = 0; i < chs.length; i++) {index = chs[i] - 'a';if (node.map[index] == null) {return 0;}node = node.map[index];}return node.path;}}public static void main(String[] args) {Trie trie = new Trie();System.out.println(trie.search("zuo"));trie.insert("zuo");System.out.println(trie.search("zuo"));trie.delete("zuo");System.out.println(trie.search("zuo"));trie.insert("zuo");trie.insert("zuo");trie.delete("zuo");System.out.println(trie.search("zuo"));trie.delete("zuo");System.out.println(trie.search("zuo"));trie.insert("zuoa");trie.insert("zuoac");trie.insert("zuoab");trie.insert("zuoad");trie.delete("zuoa");System.out.println(trie.search("zuoa"));System.out.println(trie.prefixNumber("zuo"));}}

總結

以上是生活随笔為你收集整理的leetcode 208. Implement Trie (Prefix Tree) | 208. 实现 Trie 前缀树(Java)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。