面试经典150题——字典树

ops/2025/2/13 8:39:39/

文章目录

  • 1、实现 Trie (前缀树)
    • 1.1 题目链接
    • 1.2 题目描述
    • 1.3 解题代码
    • 1.4 解题思路
  • 2、添加与搜索单词 - 数据结构设计
    • 2.1 题目链接
    • 2.2 题目描述
    • 2.3 解题代码
    • 2.4 解题思路
  • 3、单词搜索 II
    • 3.1 题目链接
    • 3.2 题目描述
    • 3.3 解题代码
    • 3.4 解题思路


对于字典树而言,之前做过类似的教程,详情可以看走入字典树一。

1、实现 Trie (前缀树)

1.1 题目链接

点击跳转到题目位置

1.2 题目描述

Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补全和拼写检查。

请你实现 Trie 类:

  • Trie() 初始化前缀树对象。
  • void insert(String word) 向前缀树中插入字符串 word 。
  • boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
  • boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。

提示:

  • 1 <= word.length, prefix.length <= 2000
  • word 和 prefix 仅由小写英文字母组成
  • insert、search 和 startsWith 调用次数 总计 不超过 3 * 104

1.3 解题代码


class TrieNode{TrieNode[] next;boolean end;TrieNode(){next = new TrieNode[26];end = false;}
}class Trie {TrieNode root;public Trie() {root = new TrieNode();}public void insert(String word) {TrieNode now = root;for(int i = 0; i < word.length(); ++i){int child = word.charAt(i) - 'a';if(now.next[child] == null){now.next[child] = new TrieNode();}now = now.next[child];}now.end = true;}public boolean search(String word) {TrieNode now = root;for(int i = 0; i < word.length(); ++i){int child = word.charAt(i) - 'a';if(now.next[child] == null){return false;}now = now.next[child]; }return now.end;}public boolean startsWith(String prefix) {TrieNode now = root;for(int i = 0; i < prefix.length(); ++i){int child = prefix.charAt(i) - 'a';if(now.next[child] == null){return false;}now = now.next[child]; }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);*/

1.4 解题思路

  1. 字典树经典题型,套用模板即可。

2、添加与搜索单词 - 数据结构设计

2.1 题目链接

点击跳转到题目位置

2.2 题目描述

请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。

实现词典类 WordDictionary :

  • WordDictionary() 初始化词典对象
  • void addWord(word) 将 word 添加到数据结构中,之后可以对它进行匹配
  • bool search(word) 如果数据结构中存在字符串与 word 匹配,则返回 true ;否则,返回 false 。word 中可能包含一些 ‘.’ ,每个 . 都可以表示任何一个字母。

提示:

  • 1 <= word.length <= 25
  • addWord 中的 word 由小写英文字母组成
  • search 中的 word 由 ‘.’ 或小写英文字母组成
  • 最多调用 104 次 addWord 和 search

2.3 解题代码

class TrieNode{TrieNode[] next;boolean end;TrieNode(){next = new TrieNode[26];end = false;}
}class WordDictionary {TrieNode root;public WordDictionary() {root = new TrieNode();}public void addWord(String word) {TrieNode now = root;for(int i = 0; i < word.length(); ++i){int child = word.charAt(i) - 'a';if(now.next[child] == null){now.next[child] = new TrieNode();}now = now.next[child];    }now.end = true;}public boolean search(String word) {Queue<TrieNode> q = new LinkedList<>();q.offer(root);for(int i = 0; i < word.length(); ++i){int len = q.size();for(int j = 0; j < len; ++j){ TrieNode x = q.peek();q.poll();if(word.charAt(i) == '.'){for(int k = 0; k < 26; ++k){if(x.next[k] != null){q.offer(x.next[k]);}}} else{int child = word.charAt(i) - 'a';if(x.next[child] != null){q.offer(x.next[child]);}} }if(q.size() == 0){return false;}}while(!q.isEmpty()){TrieNode x = q.peek();if(x.end == true){return true;}q.poll();}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);*/

2.4 解题思路

  1. 套用字典树模板。
  2. 对于搜索,因为癞子的存在,所以需要使用广度优先搜索

3、单词搜索 II

3.1 题目链接

点击跳转到题目位置

3.2 题目描述

给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words, 返回所有二维网格上的单词 。

单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。

3.3 解题代码

class Solution {int[][] dir = {{-1, 0},{1, 0},{0, 1},{0, -1},};Trie trie = new Trie();List<String> res = new ArrayList<>();public void dfs(TrieNode root, int x, int y, int m, int n, char[][] board, int vis[][], StringBuffer sb){        if(root.end == true){String str = sb.toString();res.add(str);root.end = false;}for(int i = 0; i < 4; ++i){int tx = x + dir[i][0];int ty = y + dir[i][1];if(tx < 0 || tx >= m || ty < 0 || ty >= n || vis[tx][ty] == 1){continue;}if(root.next[board[tx][ty] - 'a'] != null){vis[tx][ty] = 1;sb.append(board[tx][ty]);dfs(root.next[board[tx][ty] - 'a'], tx, ty, m, n, board, vis, sb);sb.deleteCharAt(sb.length() - 1);vis[tx][ty] = 0;}}} public List<String> findWords(char[][] board, String[] words) {int m = board.length;int n = board[0].length;int[][] vis = new int[m][n];for(int i = 0; i < words.length; ++i){trie.insert(words[i]);}StringBuffer sb = new StringBuffer();Queue<Character> q = new LinkedList<>();for(int i = 0; i < m; ++i){for(int j = 0; j < n; ++j){if(trie.root.next[board[i][j] - 'a'] != null){vis[i][j] = 1;sb.append(board[i][j]);dfs(trie.root.next[board[i][j] - 'a'], i, j, m, n, board, vis, sb);sb.deleteCharAt(sb.length() - 1);vis[i][j] = 0;}             }}return res;}
}class TrieNode{TrieNode[] next;boolean end;TrieNode(){next = new TrieNode[26];end = false;}
}class Trie {TrieNode root;public Trie() {root = new TrieNode();}public void insert(String word) {TrieNode now = root;for(int i = 0; i < word.length(); ++i){int child = word.charAt(i) - 'a';if(now.next[child] == null){now.next[child] = new TrieNode();}now = now.next[child];}now.end = true;}public boolean search(String word) {TrieNode now = root;for(int i = 0; i < word.length(); ++i){int child = word.charAt(i) - 'a';if(now.next[child] == null){return false;}now = now.next[child]; }return now.end;}public boolean startsWith(String prefix) {TrieNode now = root;for(int i = 0; i < prefix.length(); ++i){int child = prefix.charAt(i) - 'a';if(now.next[child] == null){return false;}now = now.next[child]; }return true;}
}

3.4 解题思路

1, 字典树+回溯思路
2, 如果字典树中存在该字符串,将其放入数组当中,然后将字典树中的该字符串去掉。


http://www.ppmy.cn/ops/157052.html

相关文章

某团面试题①—kudu读写流程

kudu 读写流程 前言 为什么会有kudu&#xff1f;先贴一个经典的图。 kudu诞生之前大数据的主要2种方式存储 静态数据 以hdfs引擎作为存储引擎&#xff0c;适用于高吞吐量的离线大数据分析场景&#xff0c;缺点是实现随机读写性能差&#xff0c;更新数据难 动态数据 以Hbase…

centos 7.x无法安装kong gateway 3.9X的解决方案

一、问题背景 笔者想在centos7.9上通过yum的方式安装kong gateway 3.9X&#xff0c;安装官网安装指导 curl -1sLf "https://packages.konghq.com/public/gateway-39/config.rpm.txt?distroel&codename$(rpm --eval %{rhel})" | sudo tee /etc/yum.repos.d/kong…

Elasticsearch 就业形势

聊聊 Elasticsearch 在就业市场的现状和前景。Elasticsearch 作为一种强大的搜索和分析引擎&#xff0c;近年来受到了越来越多企业和开发者的青睐。下面我们就来详细探讨一下 Elasticsearch 的就业形势。 Elasticsearch 就业形势 1. 市场需求概况 技术趋势推动需求增长 随着…

基于深度学习的人工智能量化衰老模型构建与全流程应用研究

一、引言 1.1 研究背景与意义 1.1.1 人口老龄化现状与挑战 人口老龄化是当今全球面临的重要社会趋势之一,其发展态势迅猛且影响深远。根据联合国的相关数据,1980 年,全球 65 岁及以上人口数量仅为 2.6 亿,到 2021 年,这一数字已翻番,达到 7.61 亿,而预计到 2050 年,…

DeepSeek关联WPS使用指南与案例解析

在数字化办公时代&#xff0c;人工智能&#xff08;AI&#xff09;技术正深刻地改变着我们处理文档、分析数据和进行创意表达的方式。DeepSeek作为新兴的AI技术代表&#xff0c;与办公软件巨头WPS的结合&#xff0c;为用户带来了前所未有的高效办公体验。本教程将深入探讨如何将…

基于 SpringBoot3 的 SpringSecurity6 + OAuth2 自定义框架模板

前言&#xff1a; 本人独立参考多个视频、项目以及官方文档&#xff0c;耗时7天将大致模板编写完成&#xff1b;如果有补充或者不足&#xff0c;请留言&#xff0c;希望多多支持&#xff01; &#x1f516;Gitee 项目地址&#xff1a; 基于SpringBoot3的 SpringSecurity6 OAu…

基于keepalived+GTID半同步主从复制的高可用MySQL集群

文章目录 项目架构图项目名称项目环境项目描述ip地址规划项目步骤一.安装好8台全新的centos7.9的系统&#xff0c;关闭firewalld和selinux&#xff0c;配置每台主机的静态ip地址&#xff0c;设置每台主机对应的主机名。1、关闭firewalld2.关闭seLinux3.配置每台主机静态ip地址4…

Chatbox+阿里云免费秘钥打造专属自己的deepseek桌面客户端

一、阿里云百炼创建免费api秘钥 地址&#xff1a;阿里云登录 - 欢迎登录阿里云&#xff0c;安全稳定的云计算服务平台 二、Chatbox自定义模型提供方 1、设置 2、添加 3、配置 API模式&#xff1a; OpenAI API兼容&#xff08;默认不需要修改&#xff09; 名称&#xff1a; …