Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 哈希表

  • 双指针

  • 数组

  • 字符串

  • 链表

  • 树

    • 深度遍历
    • 层序遍历
    • 树构造
    • 前缀树
      • 208. 实现 Trie (前缀树)
      • 211. 添加与搜索单词
      • 386. 字典序排数
      • 421. 数组中两个数的最大异或值
      • 440. 字典序的第K小数字
      • 648. 单词替换
      • 677. 键值映射
      • 720. 词典中最长的单词
      • 745. 前缀和后缀搜索
      • 1268. 搜索推荐系统
    • 树上倍增
  • 回溯

  • 动态规划

  • 图

  • 二分查找

  • 贪心

  • 栈&队列

  • 堆

  • 位运算

  • 数据结构设计

  • rui的精选题单

  • 笔试真题

  • LeetCode周赛

  • ACM模式输入输出
  • 数学

  • 数据结构与算法
  • 树
Nreal
2023-12-18
目录

前缀树

# 208. 实现 Trie (前缀树) (opens new window)

class Trie {

    class TrieNode{
        boolean isWord;
        TrieNode[] children;
        public TrieNode(){
            this.isWord = false;
            this.children = new TrieNode[26];
        }
    }

    TrieNode root;

    public Trie() {
        root = new TrieNode();

    }
    
    public void insert(String word) {
        TrieNode cur = root;
        for(int i=0;i<word.length();i++){
            int c = word.charAt(i)-'a';
            if(cur.children[c]==null)
                cur.children[c] = new TrieNode();
            cur = cur.children[c];
        }
        cur.isWord = true;
    }
    
    public boolean search(String word) {
        TrieNode cur = root;
        for(int i=0;i<word.length();i++){
            int c = word.charAt(i)-'a';
            if(cur.children[c]==null)
                return false;
            cur = cur.children[c];
        }
        return cur.isWord;
    }
    
    public boolean startsWith(String prefix) {
        TrieNode cur = root;
        for(int i=0;i<prefix.length();i++){
            int c = prefix.charAt(i)-'a';
            if(cur.children[c]==null)
                return false;
            cur = cur.children[c];
        }
        return true;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

# 211. 添加与搜索单词 (opens new window)

class WordDictionary {

    class TrieNode{
        boolean isEnd;
        TrieNode[] children;
        public TrieNode(){
            isEnd = false;
            children = new TrieNode[26];
        }
    }

    TrieNode root;

    public WordDictionary() {
        root = new TrieNode();
    }
    
    public void addWord(String word) {
        TrieNode cur = root;
        for(int i=0;i<word.length();i++){
            int c = word.charAt(i)-'a';
            if(cur.children[c]==null){
                cur.children[c] = new TrieNode();
            }
            cur = cur.children[c];
        }
        cur.isEnd = true;
    }
    
    public boolean search(String word) {
        return search(root,word,0);
    }
    boolean search(TrieNode cur,String word,int start){
        int n = word.length();
        if(start == n){
            return cur.isEnd;
        }
        char c = word.charAt(start);
        if(c!='.'){
            TrieNode child = cur.children[c-'a'];
            return child!=null && search(child,word,start+1);
        }
        for(int i=0;i<26;i++){
            if(cur.children[i]!=null && search(cur.children[i],word,start+1)){
                return true;
            }
        }
        return false;
    }
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

# 386. 字典序排数 (opens new window)

前缀树:

class Solution {
    public List<Integer> lexicalOrder(int n) {
        List<Integer> res = new ArrayList<>();
        int cur = 1;
        for(int i=0;i<n;i++){
            res.add(cur);
            if(cur * 10<=n){
                cur *= 10;
            }else{
                // 枚举到当前前缀的最深处了,需要回到上一层并到下一个同层节点
                while(cur%10==9 || cur>=n){
                    cur /= 10;
                }
                cur++;
            }
        }
        return res;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

回溯:

class Solution {
    List<Integer>res = new ArrayList<>();
    public List<Integer> lexicalOrder(int n) {
        for(int i=1;i<=9;i++){
            dfs(i,n);
        }
        return res;
    }
    void dfs(int cur,int limit){
        if(cur>limit)return ;
        res.add(cur);
        for(int i=0;i<=9;i++){
            dfs(cur*10+i,limit);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 421. 数组中两个数的最大异或值 (opens new window)

# 440. 字典序的第K小数字 (opens new window)

# 648. 单词替换 (opens new window)

# 677. 键值映射 (opens new window)

# 720. 词典中最长的单词 (opens new window)

# 745. 前缀和后缀搜索 (opens new window)

# 1268. 搜索推荐系统 (opens new window)

树构造
树上倍增

← 树构造 树上倍增→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式