字典树
isword是否是一个单词,字母是不是到了结尾
package Trie;
import java.util.TreeMap;
public class Trie {
private class Node{
public boolean isword;
public TreeMap<Character,Node> next; //映射
public Node(boolean isWord) {//用户传来isword 构造函数
this.isword=isword;
next=new TreeMap<>();
}
public Node() {//用户不传
this(false);//默认false
}
}
private Node root;
private int size;
public Trie() {
root=new Node();
size=0;
}
//获得Trie中存储的单词数量
public int getSize() {
return size;
}
//向Trie中添加新的单词word
//没必要保存每个节点相应字符
public void add(String word) {//添加字符串,把字符串拆成字符
Node cur=root; //一个个字符做成一个个节点
for(int i=0;i<word.length();i++) {
char c=word.charAt(i); //每次取出一个字符
if(cur.next.get(c)==null)
cur.next.put(c,new Node());
cur=cur.next.get(c);
}
if(!cur.isword) {
cur.isword=true;
size++;
}
}
Trie查询
//查询单词word是否在Trie中
public boolean contains(String word) {
Node cur=root;
for(int i=0;i<word.length();i++) {
char c=word.charAt(i);
if(cur.next.get(c)==null)
return false;
cur=cur.next.get(c);
}
return cur.isword;
}
Trie前缀搜索
//查询是否在Trie中有单词以prefix为前缀
public boolean isPrefix(String prefix) {
Node cur=root;
for(int i=0;i<prefix.length();i++) {
char c=prefix.charAt(i);
if(cur.next.get(c)==null)
return false;
cur=cur.next.get(c);
}
return true;
}
leetcode208 实现一个 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 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 class Node{
public boolean isword;
public TreeMap<Character,Node> next; //映射
public Node(boolean isWord) {//用户传来isword 构造函数
this.isword=isword;
next=new TreeMap<>();
}
public Node() {//用户不传
this(false);//默认false
}
}
private Node root;
/** Initialize your data structure here. */
public Trie() {
root=new Node();
}
/** Inserts a word into the trie. */
public void insert(String word) {
Node cur=root; //一个个字符做成一个个节点
for(int i=0;i<word.length();i++) {
char c=word.charAt(i); //每次取出一个字符
if(cur.next.get(c)==null)
cur.next.put(c,new Node());
cur=cur.next.get(c);
}
if(!cur.isword) {
cur.isword=true;
}
}
/** Returns if the word is in the trie. */
public boolean search(String word) {
Node cur=root;
for(int i=0;i<word.length();i++) {
char c=word.charAt(i);
if(cur.next.get(c)==null)
return false;
cur=cur.next.get(c);
}
return cur.isword;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
Node cur=root;
for(int i=0;i<prefix.length();i++) {
char c=prefix.charAt(i);
if(cur.next.get(c)==null)
return false;
cur=cur.next.get(c);
}
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);
*/
leetcode211 添加与搜索单词 - 数据结构设计(有通配符‘.’)
设计一个支持以下两种操作的数据结构:
void addWord(word)
bool search(word)
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。
示例:
addWord(“bad”)
addWord(“dad”)
addWord(“mad”)
search(“pad”) -> false
search(“bad”) -> true
search(".ad") -> true
search(“b…”) -> true
说明:
你可以假设所有单词都是由小写字母 a-z 组成的。
class WordDictionary {
/** Initialize your data structure here. */
private class Node{
public boolean isword;
public TreeMap<Character,Node> next; //映射
public Node(boolean isWord) {//用户传来isword 构造函数
this.isword=isword;
next=new TreeMap<>();
}
public Node() {//用户不传
this(false);//默认false
}
}
private Node root;
public WordDictionary() {
root=new Node();
}
/** Adds a word into the data structure. */
public void addWord(String word) { //同trie一样
Node cur=root; //一个个字符做成一个个节点
for(int i=0;i<word.length();i++) {
char c=word.charAt(i); //每次取出一个字符
if(cur.next.get(c)==null)
cur.next.put(c,new Node());
cur=cur.next.get(c);
}
if(!cur.isword) {
cur.isword=true;
}
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public boolean search(String word) { //遍历多叉树 递归
return match(root,word,0);
}
private boolean match(Node node,String word,int index){//node为根,基于word字符串,index字符位置
if(index==word.length())//递归到底 终止条件
return node.isword;
char c=word.charAt(index);
if(c!='.'){
if(node.next.get(c)==null) //在node中的next是否有c对应的节点
return false;
return match(node.next.get(c),word,index+1); //继续匹配后面
}
else{
for(char nextChar:node.next.keySet())//遍历node.next中对应的所有映射
if(match(node.next.get(nextChar),word,index+1))
return true;
return false;
}
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
leetcode 677 键值映射
实现一个 MapSum 类里的两个方法,insert 和 sum。
对于方法 insert,你将得到一对(字符串,整数)的键值对。字符串表示键,整数表示值。如果键已经存在,那么原来的键值对将被替代成新的键值对。
对于方法 sum,你将得到一个表示前缀的字符串,你需要返回所有以该前缀开头的键的值的总和。
示例 1:
输入: insert(“apple”, 3), 输出: Null
输入: sum(“ap”), 输出: 3
输入: insert(“app”, 2), 输出: Null
输入: sum(“ap”), 输出: 5
class MapSum {
private class Node{
// public boolean isword; 可不要
public int value;
public TreeMap<Character,Node> next; //映射
public Node(int value) { //用户传来 构造函数
this.value=value;
next=new TreeMap<>();
}
public Node() {//用户不传
this(0); //默认false
}
}
private Node root;
/** Initialize your data structure here. */
public MapSum() {
root=new Node();
}
public void insert(String word, int val) { //添加逻辑一样
Node cur=root; //一个个字符做成一个个节点
for(int i=0;i<word.length();i++) {
char c=word.charAt(i); //每次取出一个字符
if(cur.next.get(c)==null)
cur.next.put(c,new Node());
cur=cur.next.get(c);
}
cur.value=val;
}
public int sum(String prefix) {
Node cur=root;
for(int i=0;i<prefix.length();i++){
char c=prefix.charAt(i);
if(cur.next.get(c)==null)
return 0;
cur=cur.next.get(c);
}//跳出循环后,cur指向前缀最后一个节点
return sum(cur);
}
//遍历cur为根节点的多叉树,找到其中所有的表示单词的节点,加起来(递归)
private int sum(Node node){
int res=node.value;
for(char c:node.next.keySet())
res+= sum(node.next.get(c));
return res;
}
}
/**
* Your MapSum object will be instantiated and called as such:
* MapSum obj = new MapSum();
* obj.insert(key,val);
* int param_2 = obj.sum(prefix);
*/