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 ...
 
随机推荐
- windows 服务下搭建jsp运行环境
			
此处搭建的是运行环境,不是开发环境. 1, 下载sdk 并安装 1.8 http://rj.baidu.com/soft/detail/14459.html?ald 2, 配置环境变量 步 ...
 - [转]深入WPF--Style
			
Style 用来在类型的不同实例之间共享属性.资源和事件处理程序,您可以将 Style 看作是将一组属性值应用到多个元素的捷径. 这是MSDN上对Style的描述,翻译的还算中规中矩.Style(样式 ...
 - /proc/cpuinfo和/proc/meminfo来查看cpu信息与内存信息
			
#一般情况下使用root或者oracle用户查都可以. # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 --查 ...
 - 阿里云“网红"运维工程师白金:做一个平凡的圆梦人
			
他是阿里云的一位 P8 运维专家,却很有野心得给自己取花名“辟拾(P10)”:他没有华丽的履历,仅凭着 26 年的热爱与坚持,一步一个脚印踏出了属于自己的技术逆袭之路:他爱好清奇,练就了能在 20 秒 ...
 - HZOI20190814 B 不等式
			
不等式 题目大意:求解满足$L \leqslant(S×x)mod M\leqslant R$的x最小正整数解,无解输出-1 几种部分分: $L==R$,就是$ex_gcd$; 解在$1e6$以内:搜 ...
 - 关于CSS3 animation 属性在ie edge浏览器中不能工作
			
我想要给div边框加一个闪烁,所以我将css中设置如下 给想要闪烁的div加上blink类 这样在firefox,chrome下是正常显示的,但是在ie下box-shadow属性不能被正常的展现 后 ...
 - thinkphp浏览历史功能实现方法
			
这篇文章主要介绍了thinkphp浏览历史功能实现方法,可实现浏览器的浏览历史功能,是非常实用的技巧,需要的朋友可以参考下 本文实例讲述了thinkphp浏览历史功能实现方法,分享给大家供大家参考.具 ...
 - python 日记 day3
			
数据类型的概况:1.int 用于计算. 2.str 用于存储少量数据. 3.list 用于存储大量数据. 4.元祖 又叫只读列表,元素不可更改. 5. dic 用于存储关系型对象 . 6.集合 A.i ...
 - PAT甲级——A1028 List Sorting
			
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...
 - 在线模拟http-post请求
			
今天,要测试一个post请求的API,找了下,下面的网站可以直接利用起来,mark下 http://www.atool.org/httptest.php