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".
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
添加单词时注意:“长度是1”的往往比“初始为0”包含的case更多 如 c ca cat, m, mo, moc, moch, mocha。所以一般还是写“长度是1”
[思维问题]:
[一句话思路]:
单词前缀都一样的,可以用trie树,也可以用hashset,表示曾经出现过
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
所以一般还是写“长度是1”
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
不是想用hashset,而是数组根本就不能很方便地用.contains()判断存在性
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public String longestWord(String[] words) {
//ini: set,res = "", sort
Set set = new HashSet();
String res = "";
Arrays.sort(words); //cc
if (words == null) {
return res;
} //for loop : set.add(w), res ?= w
for (int i = 0; i < words.length; i++) {
if (words[i].length() == 1 || set.contains(words[i].substring(0, words[i].length() - 1))) {
res = (words[i].length() > res.length()) ? words[i] : res;
set.add(words[i]);
}
} //return
return 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 ...
- 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 ...
- 720. Longest Word in Dictionary
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- [leetcode]720. Longest Word in Dictionary字典中最长的单词
b.compareTo(a) 这个函数是比较两个值得大小,如果b比a大,那么返回1 如果小,那么返回-1,相等返回0 如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1 这个题的核心 ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
随机推荐
- (九)java位运算符
位运算符 &(与),|(或),^(异或),~(取反),<<(左移),>>(右移),>>>(无符号右移) 1:为true,0为false ...
- [leetcode]_Validate Binary Search Tree
题目:判断一棵二叉树是否合法.要求二叉树满足 左子树所有值 < 当前值 < 右子树所有值,并且所有点都满足这个条件. 思路: 1.从当前根节点判断,求根节点左子树最大值maxLeft,右子 ...
- [独孤九剑]持续集成实践(三)- Jenkins安装与配置(Jenkins+MSBuild+GitHub)
本系列文章包含: [独孤九剑]持续集成实践(一)- 引子 [独孤九剑]持续集成实践(二)– MSBuild语法入门 [独孤九剑]持续集成实践(三)- Jenkins安装与配置(Jenkins+MSBu ...
- 序列化 SqlAlchemy 的结果为 json 字符串
http://my.oschina.net/gongshang/blog/395431?p=1 在设计 RESTful 的网站时,我们总是希望 ORM 框架返回的结果是可以直接给 View 层使用的 ...
- JUnit4学习
参考:http://www.cnblogs.com/yangxia-test/p/3996120.html JUnit4是一个开源的java单元测试框架,我们只需要引入一个包,就可以使用它的功能 先说 ...
- 浅谈FPGA有限状态机
状态机几乎可以实现一切时序电路. 有限状态机(FiniteStateMachine, FSM),根据状态机的输出是否与输入有关,可分为Moore型状态机和Mealy型状态机.Moore型状态机输出仅仅 ...
- 蓝桥杯 算法训练 ALGO-60 矩阵乘法
算法训练 矩阵乘方 时间限制:1.0s 内存限制:512.0MB 问题描述 给定一个矩阵A,一个非负整数b和一个正整数m,求A的b次方除m的余数. 其中一个nxn的矩阵除m的余数得到的仍是一个 ...
- spring bean id重复覆盖的问题解决
问题: 当我们的web应用做成一个大项目之后,里面有很多的bean配置,如果两个bean的配置id是一样的而且实现类也是一样的,例如有下面两份xml的配置文档: beancontext1.xml &l ...
- zabbix snmp 协议监控 dell iRDAC
转摘:http://blog.csdn.net/wanglei_storage/article/details/52789921 http://blog.csdn.net/wanglei_storag ...
- Kibana5.6安装
Kibana5.6安装 1.下载 wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.8-linux-x86_64.tar.gz ...