首页 技术 正文
技术 2022年11月16日
0 收藏 535 点赞 4,123 浏览 2237 个字

解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793

算法来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree

实现一个 Trie (前缀树),包含 insertsearch, 和 startsWith 这三个操作。

示例:

Trie trie = new Trie();trie.insert("apple");
trie.search("apple"); // 返回 true
trie.search("app"); // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");
trie.search("app"); // 返回 true

说明:

  • 你可以假设所有的输入都是由小写字母 a-z 构成的。
  • 保证所有输入均为非空字符串。

网上找了一些解法

 class Trie {        //根节点
private TrieNode root; /** Initialize your data structure here. */
public Trie() {
//初始化根节点
root = new TrieNode();
} /** Inserts a word into the trie. */
public void insert(String word) {
TrieNode node = this.root;
//遍历
for(char c: word.toCharArray()){
if(node.children[c-'a']==null){
node.children[c-'a']=new TrieNode();
}
node = node.children[c-'a'];
}
node.item = word;
} /** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode node = this.root;
//遍历
for(char c:word.toCharArray()){
if(node.children[c-'a']==null){
return false;
}
node = node.children[c-'a'];
}
return node.item.equals(word);
} /** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode node = this.root;
//遍历
for(char c: prefix.toCharArray()){
if(node.children[c-'a']==null){
return false;
}
node = node.children[c-'a'];
}
return true; }
//定义 前缀树节点 的结构
class TrieNode{
//孩子节点,分别记录26个字母
TrieNode[] children = new TrieNode[26];
//当前的节点(叶子节点)对应的单词
String item = "";
}
}

提交后优秀解法

class Trie {
private static class TrieNode {
boolean isExist;
TrieNode[] nodes;
TrieNode() {
isExist = false;
nodes = new TrieNode[26];
}
TrieNode put(char c) {
if (nodes[c-'a'] == null) {
nodes[c-'a'] = new TrieNode();
}
return nodes[c-'a'];
}
TrieNode get(char c) {
return nodes[c-'a'];
}
} private TrieNode root; /** Initialize your data structure here. */
public Trie() {
root = new TrieNode();
} /** Inserts a word into the trie. */
public void insert(String word) {
TrieNode curr = root;
for (char c : word.toCharArray()) {
curr = curr.put(c);
}
curr.isExist = true;
} /** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode curr = root;
for (char c : word.toCharArray()) {
curr = curr.get(c);
if (curr == null) {
return false;
}
}
return curr.isExist;
} /** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode curr = root;
for (char c : prefix.toCharArray()) {
curr = curr.get(c);
if (curr == null) {
return false;
}
}
return true;
}
}每天看一些算法,没有时间去研究,只能学习。
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,468
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,096
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765