[抄题]:

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 能连续拼接出来的最长单词的更多相关文章

  1. 【Leetcode_easy】720. Longest Word in Dictionary

    problem 720. Longest Word in Dictionary 题意: solution1: BFS; class Solution { public: string longestW ...

  2. [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 ...

  3. leetcode 720. Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  4. LeetCode 720. Longest Word in Dictionary (字典里最长的单词)

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  5. 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...

  6. 720. Longest Word in Dictionary

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  7. [leetcode]720. Longest Word in Dictionary字典中最长的单词

    b.compareTo(a) 这个函数是比较两个值得大小,如果b比a大,那么返回1 如果小,那么返回-1,相等返回0 如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1 这个题的核心 ...

  8. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  9. [LeetCode] Longest Word in Dictionary 字典中的最长单词

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

随机推荐

  1. 【英语】TED视频笔记

    2014-09-22 讲话的七宗罪呢:流言蜚语.评判.消极.抱怨.借口.浮夸.固执己见. 讲话的四个要素:HAIL - 诚实,做自己,说到做到,爱. 2014-09-23 Do more of the ...

  2. 深入理解java虚拟机-第七章

    第7章 虚拟机类加载机制 类的加载的时机 加载 Loading, 连接 Linking(验证 Verfiication, 准备Preparation, 解析 Resolution) 初始化 Initi ...

  3. 微型 Python Web 框架: Bottle

    微型 Python Web 框架: Bottle 在 19/09/11 07:04 PM 由 COSTONY 发表 Bottle 是一个非常小巧但高效的微型 Python Web 框架,它被设计为仅仅 ...

  4. Vue.js 中的动态路由

    静态路由是不可以传递参数的.需要传递参数得用到动态路由 那么如何将参数作为路由呢? //在参数名前面加上 : ,然后将参数写在路由的 path 内 routes: [ //将页面组件与path指令的路 ...

  5. 提示“load System.Core failed”

    Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec8 ...

  6. js客户端UI框架

    Best jQuery UI http://b-jui.com/ jQuery EasyUI http://www.jeasyui.com/ bootstrap学习网: http://www.runo ...

  7. 如何开启 FastAdmin 的顶部导航功能?

    如何开启 FastAdmin 的顶部导航功能? FastAdmin 默认的是侧边导航,但是如果功能多的时候就有会眼花缭乱,使用顶部导航就会清晰很多. 好消息现在已经支持顶部导航,可以在demo.fas ...

  8. DD测磁盘读写性能

    1.测试磁盘的纯写入性能 dd if=/dev/zero of=/file [oracle@11g ~]$ touch ddTest[oracle@11g ~]$ time dd if=/dev/ze ...

  9. python selenium中iframe切换、window切换方法

    一.selenium中iframe切换方法: 方法一:switch_to.frame frame函数中提供了三种定位方法:by index, name, or webelement. driver.s ...

  10. Linux系统层面标配

    1.MySQL中出现存SWAP,主要会是哪些原因?--物理内存切实不足--numa导致内存分配不均,出现物理内存未使用完,就出现大量swap的使用 2.MySQ中CPU负载很高,是什么原因?给出查找的 ...