LeetCode 720. Longest Word in Dictionary (字典里最长的单词)
Given a list of strings words
representing an English Dictionary, find the longest word in words
that can be built one character at a time by other words in words
. If there is more than one possible answer, return the longest word with the smallest lexicographical order.
If there is no answer, return the empty string.
Example 1:
Input:
words = ["w","wo","wor","worl", "world"]
Output: "world"
Explanation:
The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
Example 2:
Input:
words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
Output: "apple"
Explanation:
Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
Note:
- All the strings in the input will only contain lowercase letters.
- The length of
words
will be in the range[1, 1000]
. - The length of
words[i]
will be in the range[1, 30]
.
题目标签:Hash Table
题目给了我们一个 words array,让我们找到里面 最长的word,这个word 是由字典里的其他words 由短到长一步步形成。如果遇到两个同样长度的单词,选一个 字母排序靠前的。
这道题目如果用常规方法做,会比较繁琐,速度也慢。
可以利用sort words,让所有words排序。
遍历words,遇到长度等于 1的 word 或者 这个word 去掉最后一个char 已经存在于 set:
与res 比较 留下 较长的;
把这个word 加入set。
因为是排序后的words,所以遍历顺序会是从a 到z, 从短到长。这样就会留下 最长的符合标准的word。也不用考虑 两个同样长度的 words,因为先选的就是 字母排序靠前的。
Java Solution:
Runtime beats 71.19%
完成日期:11/16/2017
关键词:HashSet,sort
关键点:把word 长度为1 的基础单词 加入 set
class Solution
{
public String longestWord(String[] words)
{
Arrays.sort(words); Set<String> set = new HashSet<>();
String res = ""; for(String word: words)
{
if(word.length() == 1 || set.contains(word.substring(0, word.length() - 1)))
{
res = word.length() > res.length() ? word : res; // keep the longer one
set.add(word);
} } return res;
}
}
参考资料:
https://discuss.leetcode.com/topic/109643/java-c-clean-code
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 720. Longest Word in Dictionary (字典里最长的单词)的更多相关文章
- [leetcode]720. Longest Word in Dictionary字典中最长的单词
b.compareTo(a) 这个函数是比较两个值得大小,如果b比a大,那么返回1 如果小,那么返回-1,相等返回0 如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1 这个题的核心 ...
- leetcode 720. Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- Leetcode720.Longest Word in Dictionary词典中最长的单词
给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最小的单词. 若无答案,则返回 ...
- 【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 ...
- [LeetCode] 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&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# 524. Longest Word in Dictionary through Deleting
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...
随机推荐
- Mysql导入导出大量数据的方法、备份恢复办法
经常使用PHP+Mysql的朋友一般都是通过phpmyadmin来管理数据库的.日常的一些调试开发工作,使用phpmyadmin确实很方便.但是当我们需要导出几百兆甚至几个G的数据库时,phpmyad ...
- 【Linux】CentOS安装Jenkins
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo sudo rpm -- ...
- CAD隐藏或显示工具条上的按钮(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::Mx_HideToolBarControl 隐藏或显示工具条上的按钮.详细说明如下: 参数 说明 IN LPCTSTR pszTool ...
- nginx_gzip压缩提升网站的传输速度
gzip on; gzip_min_length 1k; gzip_buffers 16k; #gzip_http_version 1.0; gzip_comp_level ; gzip_types ...
- idea_复制包名类名
- JAVA基础——toString()方法
toString()方法返回反映这个对象的字符串 因为toString方法是Object里面已经有了的方法,而所有类都是继承Object,所以“所有对象都有这个方法”. 它通常只是为了方便输出,比如S ...
- 打造完美的ImageLoader——LruCache+DiskLruCache
做android应用少不了要和网络打交道,在我刚开始学android的时候总是处理不好网络图片的加载,尤其是图片乱跳的问题,后来发现了各种缓存图片的方法:本地缓存.软引用.LruCache.... 我 ...
- 诊断:expdp导出时遇到错误ORA-31693和ORA-00922
11.2.0.1使用数据泵expdp导出时,如果使用parallel,可能会遇到 ORA-: Table data object "OWNER"."TABLE" ...
- Python,subprocess模块(补充)
1.subprocess模块,前戏 res = os.system('dir') 打印到屏幕,res为0或非0 os.popen('dir') 返回一个内存对象,相当于文件流 a = os.popen ...
- gitlab root 账号 忘记密码如何重置
shell>cd /home/git/gitlabshell> su gitshell>bundle exec rails console productionirb(main):0 ...