当前位置:网站首页>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 "";
}
};
边栏推荐
- [untitled]
- 吴恩达《机器学习》课程总结(11)_支持向量机
- [idea] idea formatting code skills
- SQL reported an unusual error, which confused the new interns
- Software engineering job design (1): [personal project] implements a log view page
- [AI application] detailed parameters of NVIDIA Tesla v100s-pcie-32gb
- MySQL read / write separation configuration
- [PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation
- 互联网业衍生出来了新的技术,新的模式,新的产业类型
- Arduino uno realizes simple touch switch through direct detection of capacitance
猜你喜欢

Chenyun pytorch learning notes_ Build RESNET with 50 lines of code

每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,100行源码揭秘!

MySQL分表查询之Merge存储引擎实现

Sécurité, économie de carburant et protection de l'environnement chameau

Alchemy (7): how to solve problems? Only reconstruction

现代编程语言:zig
![[microservices sentinel] sentinel data persistence](/img/9f/2767945db99761bb35e2bb5434b44d.png)
[microservices sentinel] sentinel data persistence

Technical debt wall: a way to make technical debt visible and negotiable

Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)

【无标题】
随机推荐
剑指 Offer 61. 扑克牌中的顺子
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
TIME_WAIT过多的解决办法
[black apple series] m910x perfect black apple system installation tutorial – 2 making system USB disk -usb creation
CharSequence初探
Arduino uno realizes simple touch switch through direct detection of capacitance
[黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation
Differences and functions between intranet IP and public IP
Transmitting and receiving antenna pattern
Matlab基本函数 length函数
Oracle数据库的启停
现代编程语言:zig
软件工程作业设计(1): [个人项目] 实现一个日志查看页面
Is the securities registration account safe? Is there any risk?
炼金术(7): 何以解忧,唯有重构
[VIM] tutorial, common commands, efficient use of vim editor
【论文阅读|深读】SDNE:Structural Deep Network Embedding
积分体系和营销活动结合在一起有哪些玩法
安全省油環保 駱駝AGM啟停電池魅力十足
MongoDB-在windows电脑本地安装一个mongodb的数据库