【Leetcode_easy】720. Longest Word in Dictionary
problem
720. Longest Word in Dictionary
题意:
solution1: BFS;
class Solution {
public:
string longestWord(vector<string>& words) {
string res = "";
unordered_set<string> s(words.begin(), words.end());
queue<string> q;
for(auto word:words)
{
if(word.size()==) q.push(word);
}
int maxLen = ;
while(!q.empty())
{
string t = q.front();
q.pop();
if(t.size()>maxLen)
{
maxLen = t.size();
res = t;//err....
}
else if(t.size()==maxLen) res = min(res, t);
for(char ch='a'; ch<='z'; ++ch)//err...
{
t.push_back(ch);
if(s.count(t)) q.push(t);
t.pop_back();
}
}
return res;
}
};
solution2:
class Solution {
public:
string longestWord(vector<string>& words) {
string res = "";
unordered_set<string> s(words.begin(), words.end());
int maxLen = ;
for (auto word:words)
{
if(word.size()==) helper(s, word, maxLen, res);
}
return res;
}
void helper(unordered_set<string>& s, string word, int& maxLen, string& res) {
if(word.size()>maxLen)
{
maxLen = word.size();
res = word;
}
else if(word.size()==maxLen) res = min(res, word);
for(char ch = 'a'; ch<='z'; ++ch)
{
word.push_back(ch);
if(s.count(word)) helper(s, word, maxLen, res);
word.pop_back();
}
}
};
参考
1. Leetcode_easy_720. Longest Word in Dictionary;
2. Grandyang;
完
【Leetcode_easy】720. Longest Word in Dictionary的更多相关文章
- 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...
- [LeetCode&Python] Problem 720. Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- leetcode 720. Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- 720. Longest Word in Dictionary 能连续拼接出来的最长单词
[抄题]: Given a list of strings words representing an English Dictionary, find the longest word in wor ...
- LeetCode 720. Longest Word in Dictionary (字典里最长的单词)
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- 【Leetcode_easy】953. Verifying an Alien Dictionary
problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...
- 【Leetcode_easy】687. Longest Univalue Path
problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完
- 【Leetcode_easy】674. Longest Continuous Increasing Subsequence
problem 674. Longest Continuous Increasing Subsequence solution class Solution { public: int findLen ...
- 【Leetcode_easy】594. Longest Harmonious Subsequence
problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...
随机推荐
- 创建型模式(一) 单例模式(Singleton)
一.动机(Motivation) 在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性.以及良好的效率. 如何绕过常规的构造器,提供一种机制来保证一个类只 ...
- Java8新特性--CompletableFuture
并发与并行 Java 5并发库主要关注于异步任务的处理,它采用了这样一种模式,producer线程创建任务并且利用阻塞队列将其传递给任务的consumer.这种模型在Java 7和8中进一步发展,并且 ...
- tomcat配置虛擬路徑
1.server.xml设置 打开Tomcat安装目录,在server.xml中<Host>标签中,增加<Context docBase="硬盘目录" path= ...
- 关于File.separator 文件路径:wind与linux下路径问题 .
最近有个在页面上传Excel文件至服务器指定目录并进行数据校验.最后入库及进行进一步处理的应用情境,我写好代码在模拟环境下测试,完全没问题:但客户试用的时候,却老是报告“No such file or ...
- [Luogu] 封锁阳光大学
https://www.luogu.org/problemnew/show/P1330 #include <cstdio> #include <cstring> #includ ...
- python中如何给散点图上面的特定点做标记
今天想在散点图的某些特定的点外面画圆圈标记,从下面的文章找到一些灵感,只要在原来的散点图上面给指点添加相应的标志,设置其透明度就可以实现该想法. 顺便复习下散点图的用法. 大家平时为了直观地显示数据的 ...
- webkit vs v8
我们知道不同浏览器用的不同的渲染引擎: Tridend(IE).Gecko(FF).WebKit(Safari,Chrome,Andriod浏览器) 当然 Chrome 重构了一下 WebKit 然后 ...
- java中int 和String相互转换
一.String转为int int i=Integer.parseInt(string):int i=Integer.valueOf(s).intValue(); 二.int转为String Stri ...
- (转)libvirt和qemu编译安装
借鉴:https://www.cnblogs.com/grglym/p/8053553.html 借鉴:http://blog.chinaunix.net/uid-31410005-id-577189 ...
- 5分钟学会如何创建spring boot项目
上一篇博客说了如何创建spring boot项目,但是有些同学会觉得有点麻烦,有没有什么快速学会能快速创建spring boot项目的方法,答案是肯定的.接下来我们就一起来快速创建一个spring b ...