当前位置:网站首页>Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
2022-06-27 22:04:00 【我不是萧海哇~~~~】

给出一个字符串数组 words 组成的一本英语词典。返回 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"
提示:
- 1 <= words.length <= 1000
- 1 <= words[i].length <= 30
- 所有输入的字符串 words[i] 都只包含小写字母。
主要思路:先自定义排序,然后定义个二维数组用来存储长度一致的单词
之后判断长度是否连续,内容是否递增即可
Code:
class Solution {
public:
static bool cmp(const string &s1,const string &s2)
{
if(s1.length()==s2.length())
return s1<s2;
return s1.length()<s2.length();
}
string longestWord(vector<string>& words) {
sort(words.begin(),words.end(),cmp);
int len=1;
int maxlen=words[words.size()-1].length();
vector<vector<string>>vec;
int start_index=0;
for(int i=1;i<=maxlen;i++)
{
bool flag=false;//用来判断长度是否是连续
bool flag2=false;//用来判断内容是否递增
vector<string>temp;
for(int j=start_index;j<words.size();j++)
{
if(words[j].length()==i)
{
flag=true;
//cout<<words[j]<<endl;
if(i!=1)
{
vector<string>sub=vec[i-2];
string t=words[j].substr(0,i-1);
if(find(sub.begin(),sub.end(),t)!=sub.end())
{
// cout<<words[j]<<endl;
temp.push_back(words[j]);
flag2=true;
}
}
if(i==1)
{
flag2=true;
temp.push_back(words[j]);
}
}
else
{
start_index=j;
break;
}
}
if(flag&&flag2)
{
if(i==maxlen)
return temp[0];
}
if(!flag2 || !flag )
{
if(vec.size())
{
return vec[i-2][0];
}
else
return "";
}
else
{
vec.push_back(temp);
}
}
return "";
}
};
边栏推荐
- [读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
- Local visualization tool connects to redis of Alibaba cloud CentOS server
- C语言malloc函数的功能及用法
- IIC communication protocol for single chip microcomputer
- 炼金术(8): 开发和发布的并行
- 哪个证券公司炒股开户是比较好,更安全靠谱的
- Alchemy (6): iteratable models and use cases
- Solve the cross domain problem of the new version of chrome: Cookie loss and samesite attribute problem "recommended collection"
- 吴恩达《机器学习》课程总结(11)_支持向量机
- [idea] idea formatting code skills
猜你喜欢

Flutter series: Transformers in flutter

How to quote Chinese documents when writing a foreign language?

智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维

Safe, fuel-efficient and environment-friendly camel AGM start stop battery is full of charm
![[digital ic/fpga] detect the position of the last matching sequence](/img/67/a1b575aa9b63892ed585d39e615c58.png)
[digital ic/fpga] detect the position of the last matching sequence

夏日的晚会
![[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation](/img/9e/f08ce0729c89b0205c0ac47c523ad7.png)
[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation

本地可视化工具连接阿里云centOS服务器的redis
![Using two stacks to implement queues [two first in first out is first in first out]](/img/de/07297816f1a44d41389bb45d012c80.png)
Using two stacks to implement queues [two first in first out is first in first out]

MySQL分表查询之Merge存储引擎实现
随机推荐
Character interception triplets of data warehouse: substrb, substr, substring
Alchemy (3): how to do a good job in interfacing a business process
本地可视化工具连接阿里云centOS服务器的redis
翻译(4): 文本自动完成的匹配规则
华泰证券在网上开户安全吗?
The Internet industry has derived new technologies, new models and new types of industries
ValidateRequest=”false” 是做什么的「建议收藏」
[idea] idea formatting code skills
现代编程语言:zig
Using two stacks to implement queues [two first in first out is first in first out]
Alchemy (9): simple but not simple, never-ending test -- always_ run
单片机之IIC通信协议「建议收藏」
Promise是什么
mysql数据库旅游管理系统_JSP+MySQL基于ssm的旅游管理系统[通俗易懂]
积分体系和营销活动结合在一起有哪些玩法
Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)
Chenyun pytorch learning notes_ Build RESNET with 50 lines of code
SQL reported an unusual error, which confused the new interns
What is promise
At the beginning of reading English literature, I would like to ask you how you should read it in the first place?