题目

https://leetcode.com/problems/longest-word-in-dictionary/

题解

建立一个 Trie,在 insert 的过程中,除最后一个节点外,如果一路上每一个节点都 isEnd,则将其返回值 diff 设置为 false,表明它的差异可以被容忍,可参与最后的长度比较。

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;public Trie() {node = new Node();}public boolean insert(String word) {char[] chars = word.toCharArray();Node cur = node;boolean diff = false;for (int i = 0; i < chars.length; i++) {if (cur.get(chars[i]) != null) {cur = cur.get(chars[i]);if (!cur.isEnd) diff = true;} else {cur = cur.put(chars[i]);if (i != word.length() - 1) diff = true;}if (i == chars.length - 1) cur.isEnd = true;}return diff;}
}class Solution {public String longestWord(String[] words) {Arrays.sort(words);Trie trie = new Trie();String result = "";for (String word : words) {if (!trie.insert(word)) result = word.length() > result.length() ? word : result;}return result;}
}

leetcode 720. Longest Word in Dictionary | 720. 词典中最长的单词(Trie前缀树)相关推荐

  1. 【LeetCode】720. 词典中最长的单词 【前缀树】

    题目链接:https://leetcode-cn.com/problems/longest-word-in-dictionary/ 题目描述 给出一个字符串数组words组成的一本英语词典.从中找出最 ...

  2. 字典树/Trie/前缀树-LeetCode总结:720词典中最长的单词;127. 单词接龙;677. 键值映射;面试题 17.17. 多次搜索;648. 单词替换

    MyTrie结构体和相关操作函数 typedef struct MyTrie {bool is_word;vector<MyTrie*> next;MyTrie():is_word(fal ...

  3. 720. 词典中最长的单词

    链接:720. 词典中最长的单词 题解: class Solution { private:struct Trie {int end;std::set<std::string> words ...

  4. 每日一练 LeetCode:E720. 词典中最长的单词

    题目 给出一个字符串数组 words 组成的一本英语词典.返回 words 中最长的一个单词,该单词是由 words 词典中其他单词逐步添加一个字母组成. 若其中有多个可行的答案,则返回答案中字典序最 ...

  5. #力扣LeetCode720. 词典中最长的单词 @FDDLC

    题目描述: 720. 词典中最长的单词 - 力扣(LeetCode) (leetcode-cn.com) 自测用例: ["w","wo","wor&q ...

  6. LeetCode 720. 词典中最长的单词(Trie树)

    1. 题目 给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最小的单词. 若无 ...

  7. leetcode 676. Implement Magic Dictionary | 676. 实现一个魔法字典(DFS+Trie 前缀树)

    题目 https://leetcode.com/problems/implement-magic-dictionary/description/ 题解 题意理解 前缀树问题,大意是是让你在字典中找到是 ...

  8. Leetcode典型题解答和分析、归纳和汇总——T208(实现Trie前缀树)

    问题描述: 实现一个Trie前缀树,包含insert.search和startsWith这三个操作. 问题分析: 这类的题目与堆栈的最小元素查找类似,将所有功能进行集中处理. 首先我们需要明确一下tr ...

  9. LeetCode 524. Longest Word in Dictionary through Deleting

    题目: Given a string and a string dictionary, find the longest string in the dictionary that can be fo ...

最新文章

  1. AI寒冬论作者:通用人工智能仍是白日梦
  2. 第七章 培养负责的习惯
  3. Spring 详解(四):Spring MVC
  4. 启明云端技术社区之星--张梦玉
  5. 聊聊JVM(三)两种计算Java对象大小的方法
  6. 图像分类数据库_图像分类器-使用僧侣库对房屋房间类型进行分类
  7. 550 5.7.1 Client does not have permissions to send as this sender
  8. netty中的引导Bootstrap服务端
  9. SpringBoot 2.1.5(12)--- Spring Boot 特性上
  10. pythonflask开发web网页编辑_总结Python+Flask+MysqL的web建设技术过程
  11. 利用TestDriven.net和NUnit进行单元测试(转)
  12. 控制台打印vue实例
  13. Redis基本类型之Set类型
  14. 实验吧——安全杂项之“A记录”详解
  15. CAP 原理和分布式矩阵的应用
  16. android 扫描二维码黑屏,android--zxing返回扫描界面出现黑屏的解决方案
  17. curl php 宝塔 开启_宝塔安装php失败
  18. TextRank算法实践
  19. 链式存储【C语言单链表】
  20. MySql 根据身份证号判断年龄所属省份与性别男女

热门文章

  1. UVA - 817According to Bartjens
  2. YOLO目标检测算法
  3. 安卓入门系列-09一个实战小项目(备忘录Memo)
  4. Python实战-获取鼠标键盘事件
  5. java 微信api框架_Java架构学习(五十二)使用框架开发微信微信框架介绍整合微信开发框架WxJava使用微信返回消息模板接口使用WxJava框架发送模板消息http协议参数转义知识...
  6. STL系列之一 deque双向队列
  7. 如何打造不怕被嗅探的3389登陆
  8. MySQL(五)MySQL事务
  9. 硬核分析|腾讯云原生OS内存回收导致关键业务抖动问题
  10. KCL:声明式的云原生配置策略语言