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的更多相关文章

  1. 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...

  2. [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 ...

  3. leetcode 720. Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  4. 720. Longest Word in Dictionary 能连续拼接出来的最长单词

    [抄题]: Given a list of strings words representing an English Dictionary, find the longest word in wor ...

  5. LeetCode 720. Longest Word in Dictionary (字典里最长的单词)

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  6. 【Leetcode_easy】953. Verifying an Alien Dictionary

    problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...

  7. 【Leetcode_easy】687. Longest Univalue Path

    problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完

  8. 【Leetcode_easy】674. Longest Continuous Increasing Subsequence

    problem 674. Longest Continuous Increasing Subsequence solution class Solution { public: int findLen ...

  9. 【Leetcode_easy】594. Longest Harmonious Subsequence

    problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...

随机推荐

  1. PL/SQL复合类型

    一.PL/SQL记录:一条记录. 可简化单行多列的数据的处理.当使用pl/sql记录时,应用开发人员即可以自定义记录类型和记录变量,也可以使用%rowtype属性直接定义记录变量. 1.当使用自定义的 ...

  2. vs2017 c# 控制台 输出中文显示问号 ; vs2017 c# 控制台 输出中文显示乱码

    问题: 解决: 在main方法最前面加一句就OK了! Console.OutputEncoding = Encoding.GetEncoding("gbk"); 或者 Consol ...

  3. Spring 整合 Quartz框架(定时任务)

    Maven 无法下载 Quartz 依赖,去官网下载 http://www.quartz-scheduler.org/downloads/ Quartz 官方手册:https://www.w3csch ...

  4. ModbusRTU模式和结束符(转)

    Modbus RTU模式的协议字段 起始位 设备地址 功能码 数据 CRC校验 结束符 至少3.5个字符 8bit 8bit N*8bit 16bit 至少3.5个字符 Modbus协议RTU模式要求 ...

  5. 07_gitee源码参考

    Django REST framework Tutorial 教程 码云:https://gitee.com/venicid/tutorial-api

  6. 历年NOIP题

    做了几天远古老题,发现不可做,于是咕掉..转而从2005开始.. 1997: P1549 棋盘问题(2):搜索,优化搜索顺序,对于第一行第一列先搜小的(但是其实这样是错的,仅仅能过原题) 加强版咕. ...

  7. ubuntu 停留开机界面解决方法

    1 问题 Ubuntu 启动时卡在开机界面上 2 修复 $ sudo apt-get -y reinstall ubuntu-desktop*

  8. 三十三.mysqldump 实时增量备份 、innobackupex

    1.数据库备份与恢复 逻辑备份工具 mysqldump 使用mysql 恢复数据库   1.1备份MySQL服务器上的所有库 ]# mysqldump -u root -p123456 --all-d ...

  9. 2G以上的大文件如何上传

    无法上传大文件是因为php.ini配置有限制了,这样限制了用户默认最大为2MB了,超过了就不能上传了,如果你确实要上传我们可以按下面方法来处理一下. 打开php.ini, 参数  设置  说明 fil ...

  10. Python之yield语法

    生成器与yield 函数使用yield关键字可以定义生成器对象.生成器是一个函数.它生成一个值的序列,以便在迭代中使用,例如: def countdown(n): print('倒计时:%s' % n ...