这个方法对于数据量小的可行,大量的数据不可行

class Solution {public:string longestWord(vector<string>& words) {set<string> mySet;string res;int MaxSize = 0;for(string str : words){mySet.insert(str);}for(auto it = mySet.begin();it != mySet.end();it++){for(int i = 0;i < words.size();i++){string str = words[i];int count = 0;for(int j = 1;j <= str.size();j++){if(mySet.find(str.substr(0,j)) == mySet.end()){break;}else{count++;}}if(count == str.size()){if(res == ""){res = str;MaxSize = count;}else if(MaxSize < str.size()){res = str;MaxSize = res.size();}else if(MaxSize == str.size()){res = (res < str ? res : str);}}}}return res;}
};


用排序来加快查找的速度。
如果以后做题的时间减不下来,可以从排序的角度去考虑

class Solution {public:
static    bool compareSize(const string& a,const string& b){if(a.size() != b.size()){return a.size() < b.size();}//else return a < b;else return a > b;//当字典序一样,字母序小的排在后面}string longestWord(vector<string>& words) {sort(words.begin(),words.end(),compareSize);unordered_set<string> us;for(auto & str:words){us.insert(str);}string result;for(int i = words.size()-1;i >= 0;--i){result = words[i];int len = result.size();while(len--){result.pop_back();if(us.find(result) == us.end()){break;}}if(len == 0){result = words[i];break;}else result = "";}return result;}
};

2022-1-22 Leetcode 720.词典中最长的单词相关推荐

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

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

  2. LeetCode 720. 词典中最长的单词

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

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

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

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

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

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

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

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

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

  7. 【LeetCode】词典中最长的单词(附集合操作、lamda用法)

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

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

    题目 https://leetcode.com/problems/longest-word-in-dictionary/ 题解 建立一个 Trie,在 insert 的过程中,除最后一个节点外,如果一 ...

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

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

最新文章

  1. Redis集群Twemproxy
  2. [转]Android NDK几点回调方式
  3. 文本挖掘简介及软件安装
  4. java 接口与抽象类的区别
  5. java新手笔记1 Hello World!
  6. JVM性能调优(转)
  7. JAVA程序设计----IO流(下)
  8. 通过用 .NET 生成自定义窗体设计器来定制应用程序
  9. 515Nod 1126 求递推序列的第n项【矩阵快速幂】
  10. Spark消费kafka任务卡死:Marking the coordinator xxx:9092 (id: 2147483647 rack: null) dead for group xxx
  11. Linux使用cpuset设置CPU独占
  12. 【java】中缀表达式转后缀表达式 java实现
  13. 蓝桥杯备赛第一天-138译码器
  14. BZOJ 4565 字符合并 (区间状压dp)
  15. html中怎么写艺术字,用CSS设计艺术字
  16. 【新书速递】流量运营教科书
  17. python精彩语句(来日方长,慢慢录入)
  18. CSS3实战-文字篇
  19. 如何打造优秀的个人博客
  20. dynamic linke library example (C/C++)

热门文章

  1. 详解互联网APP架构1.0
  2. 数据库开发及ADO.NET(6)——新增 - Inert into Table(col,col2...)values(data1,data2...)
  3. 关于软件测试的一些基本知识
  4. matlab计算层次法的权重,层次分析法计算权重在matlab中的实现
  5. java怎么重载两次_[Core Java® for the Impatient]重载Java2
  6. 我也学人开个blog玩玩,嘻嘻
  7. 通过JavaScript自由切换iframe
  8. ESP8266学习之路 十三 (SPI读取max6675)
  9. 矩阵乘法的纯Python实现 | 离开Python库!!
  10. 使用Spacedesk实现局域网内任意设备作为电脑拓展屏