720. Longest Word in Dictionary
static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
string longestWord(vector<string>& words)
{
unordered_set<string> siset(words.begin(),words.end());
string res;
for(string s:words)
{
if(s.length()>res.length()||(s.length()==res.length()&&s<res))
{
if(judgein(siset,s))
res=s;
}
else
continue;
}
return res;
} bool judgein(unordered_set<string> &siset,string s)
{
while(!s.empty())
{
if(siset.find(s)!=siset.end())
s.pop_back();
else
return false;
}
return true;
}
};
用一个set将原容器中的字符串扫描进去,然后顺序扫描字符串容器,只有当当前字符串长度长于res或者长度和res相等但是小于res时,才调用判定函数判定该字符串是否能由容器中的单词序列构成,若判定结果为真,则更新res。
720. Longest Word in Dictionary的更多相关文章
- 【Leetcode_easy】720. Longest Word in Dictionary
problem 720. Longest Word in Dictionary 题意: solution1: BFS; class Solution { public: string longestW ...
- [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 ...
- leetcode 720. Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- 720. Longest Word in Dictionary 能连续拼接出来的最长单词
[抄题]: Given a list of strings words representing an English Dictionary, find the longest word in wor ...
- LeetCode 720. Longest Word in Dictionary (字典里最长的单词)
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...
- [leetcode]720. Longest Word in Dictionary字典中最长的单词
b.compareTo(a) 这个函数是比较两个值得大小,如果b比a大,那么返回1 如果小,那么返回-1,相等返回0 如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1 这个题的核心 ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- Longest Word in Dictionary through Deleting - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Longest Word in Dictionary through Deleting - LeetCode 注意点 长度一样的字符串要按字典序返回较小的 ...
随机推荐
- linux 重定向命令详解(如1>/dev/null 2>&1)
基础 0:表示标准输入stdin 1:表示标准输出stdout,系统默认为1,可省略(即1>/dev/null等价于>/dev/null) 2:表示标准错误stderr >:表示重定 ...
- minSdk(API 26) > deviceSdk(API 19)解决方式
运行项目时出现“minSdk(API 26) > deviceSdk(API 19)”的提示,因为我用的是手机是sdk(API19)的,而项目要求是最低版本是minSdk(API 26),在我的 ...
- 转移动APP测试实践
http://blog.csdn.net/hgstclyh/article/details/53115325
- [剑指Offer]48-最长不含重复字符的子字符串(递归思想,循环实现)
题意 如题,字符串只含a-z,输出该子串长度.例:"arabcacfr",输出4. 解题思路 递归思想 计f(i)为以第i个字符结尾的最长不含重复字符的子串长度. 状态转移:计d为 ...
- C++ 中的RTTI机制详解
前言 RTTI是”Runtime Type Information”的缩写,意思是运行时类型信息,它提供了运行时确定对象类型的方法.RTTI并不是什么新的东西,很早就有了这个技术,但是,在实际应用中使 ...
- Java 获取CPU、内存、外网IP等硬件信息
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.Input ...
- 线特征---LBD算法(三)
上一节主要是介绍LSD算法理论,这节主要是介绍LBD算法. 参考文章:An efficient and robust line segment matching approach based on L ...
- html5的鼠标拖拽
鼠标拖拽 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- Struts框架的数据封装二之模型驱动方式
Struts2中提供了两类数据封装的方式? * 第二种方式:模型驱动 > 使用模型驱动的方式,也可以把表单中的数据直接封装到一个JavaBean的对象中,并且表单的写法和之前的写法没有区别! & ...
- sql backup
create or replace procedure P_updateasbegin update security_price p set p.closing_price = (select MI ...