当前位置:网站首页>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 "";
}
};
边栏推荐
- Transmitting and receiving antenna pattern
- Translation (4): matching rules for automatic text completion
- Sécurité, économie de carburant et protection de l'environnement chameau
- Alchemy (4): mental model of programmers
- 【论文阅读|深读】SDNE:Structural Deep Network Embedding
- [PCL self study: pclplotter] pclplotter draws data analysis chart
- Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
- 剑指 Offer 61. 扑克牌中的顺子
- [读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
- [AI application] detailed parameters of NVIDIA Tesla v100s-pcie-32gb
猜你喜欢

Chenyun pytorch learning notes_ Build RESNET with 50 lines of code

Sentinel

Summary of wuenda's machine learning course (11)_ Support vector machine

【无标题】

MATLB|改进的前推回代法求解低压配电网潮流
![[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK](/img/7b/8b3619d7726fdaa58da46b0c8451a4.png)
[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK

Logging log usage

Sell notes | brief introduction to video text pre training

MongoDB-在windows电脑本地安装一个mongodb的数据库

剑指 Offer 61. 扑克牌中的顺子
随机推荐
Sell notes | brief introduction to video text pre training
表单form 和 表单元素(input、select、textarea等)
Code neatness -- function
[VIM] tutorial, common commands, efficient use of vim editor
快速掌握grep命令及正则表达式
Is it safe for Huatai Securities to open an account online?
C语言malloc函数的功能及用法
TIME_WAIT过多的解决办法
Solve the cross domain problem of the new version of chrome: Cookie loss and samesite attribute problem "recommended collection"
Mongodb- install a mongodb database locally on the windows computer
Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
Grab those duplicate genes
SCU|通过深度强化学习进行微型游泳机器人的步态切换和目标导航
技术的极限(11): 有趣的编程
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation
Arduino UNO通过电容的直接检测实现简易触摸开关
每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,100行源码揭秘!
Flutter series: Transformers in flutter
互联网的发展为产业的变革和转型提供了新的解决方案