Leetcode720.Longest Word in Dictionary词典中最长的单词
给出一个字符串数组words组成的一本英语词典。从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成。若其中有多个可行的答案,则返回答案中字典序最小的单词。
若无答案,则返回空字符串。
示例 1:
输入: words = ["w","wo","wor","worl", "world"] 输出: "world" 解释: 单词"world"可由"w", "wo", "wor", 和 "worl"添加一个字母组成。
示例 2:
输入: words = ["a", "banana", "app", "appl", "ap", "apply", "apple"] 输出: "apple" 解释: "apply"和"apple"都能由词典中的单词组成。但是"apple"得字典序小于"apply"。
注意:
- 所有输入的字符串都只包含小写字母。
- words数组长度范围为[1,1000]。
- words[i]的长度范围为[1,30]。
bool cmp1(string a, string b)
{
return a.size() > b.size();
}
bool cmp2(string a, string b)
{
return a < b;
}
class Solution {
public:
string longestWord(vector<string>& words) {
int len = words.size();
if(len <= 1)
return len == 0? "":words[0];
vector<string> res;
map<string, int> check;
for(int i = 0; i < len; i++)
{
check[words[i]] = 1;
}
sort(words.begin(), words.end(), cmp1);
int MAXsize = 0;
for(int i = 0; i < len; i++)
{
int size = words[i].size();
bool flag = true;
if(MAXsize != 0 && MAXsize > size)
continue;
for(int j = 0; j < size - 1; j++)
{
string temp = "";
for(int k = 0; k <= j; k++)
{
temp += words[i][k];
}
if(check[temp] != 1)
{
flag = false;
break;
}
}
if(flag == true)
{
MAXsize = max(MAXsize, size);
res.push_back(words[i]);
}
}
if(res.size() == 0)
return "";
sort(res.begin(), res.end(), cmp2);
return res[0];
}
};
Leetcode720.Longest Word in Dictionary词典中最长的单词的更多相关文章
- [leetcode]720. Longest Word in Dictionary字典中最长的单词
b.compareTo(a) 这个函数是比较两个值得大小,如果b比a大,那么返回1 如果小,那么返回-1,相等返回0 如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1 这个题的核心 ...
- Java实现 LeetCode 720 词典中最长的单词(字典树)
720. 词典中最长的单词 给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最 ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- leetcode 720. 词典中最长的单词
/* 1.hashtable 把每个字符串都放到hashtable中 a.排序 长度不同,长的放在前面,长度相同,字典序小的放在前面 b.不排序 遍历数组,对于每个字符串判断它的所有前缀是否都在has ...
- Leetcode字典树-720:词典中最长的单词
第一次做leetcode的题目,虽然做的是水题,但是菜鸟太菜,刚刚入门,这里记录一些基本的知识点.大佬看见请直接路过. https://leetcode-cn.com/problems/longest ...
- Longest Word in Dictionary through Deleting - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Longest Word in Dictionary through Deleting - LeetCode 注意点 长度一样的字符串要按字典序返回较小的 ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【Leetcode_easy】720. Longest Word in Dictionary
problem 720. Longest Word in Dictionary 题意: solution1: BFS; class Solution { public: string longestW ...
随机推荐
- _itoa _itow _itot atoi atof atol
函数原型: char *_itoa( int value, char *string, int radix ); //ANSI wchar_t * _itow( int value, wchar_t ...
- 解决wordpress 5.3更新后Uncaught Typeerror: $ is not a function
本文不再更新,可能存在内容过时的情况,实时更新请移步原文地址:解决wordpress 5.3更新后Uncaught Typeerror: $ is not a function: 本文通过插件的办法解 ...
- Joomla - akeeba backup(joomla网站备份、迁移扩展)
在所有 joomla 的网站中,如果只允许安装一个扩展,估计超过90%的人都会选择 akeeba backup,这基本是每个joomla都必备的一个扩展: akeeba backup 的更多资料可以到 ...
- spring cloud深入学习(九)-----配置中心服务化和高可用
在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...
- Pycharm 添加第三方库
坏境:pycharm专业版 点击:file ——>setting ——>Project:python source code ——>Project interpreter ——> ...
- PAT甲级——A1040 Longest Symmetric String
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
- ORACLE忘记sys密码
1.win+R打开dos窗口cmd 2.输入 sqlplus/nolog出现 3.输入 conn / as sysdba 出现 4.修改密码 alter user 用户 identified by 新 ...
- Python - 基本数据类型及其常用的方法之元组
元组 特点:一级元素无法被修改,且不能被增加或者删除. 基本操作: tu = (11, 22, ["aiden", 33, ("qwe", 11)], 77) ...
- Linux时间介绍
Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟.系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟, ...
- Linux命令CURL用法
Curl是一个命令行方式下传输数据的开源传输工具,支持多种协议包括:FTP,HTTP,HTTPS,IMAP,POP3,TELNET等.同样支持HTTP POST方法,PUT方法,FTP上传,cooki ...