b.compareTo(a)

这个函数是比较两个值得大小,如果b比a大,那么返回1

如果小,那么返回-1,相等返回0

如果比较的是字符串,那么比较字典编纂顺序,b靠前返回-1,靠后返回1

这个题的核心虽然是hashtable,但是这个方法还是很重要的,因为自己实现比较字符串很麻烦

/*
用一个Hashset来判断子串是不是在集合里
然后遍历出最长的就行
长度相同的用compareTo方法判断谁的字典顺序靠前
这个题最难的感觉其实是不知道compareTo方法,自己实现还挺麻烦的
*/
public String longestWord(String[] words) {
//构建set
Set<String> set = new HashSet<>();
for (String s :
words) {
set.add(s);
}
//记录最大长度字符串
String res = "";
//遍历判断
for (String s :
words) {
//两种情况值得更新,一种是长度更长,一种是长度一样但是顺序靠前
if (s.length() > res.length() || (s.length() == res.length() && s.compareTo(res) < 0)) {
//判断字符串的子串在不在集合里
boolean judge = true;
for (int i = s.length() - 1; i > 0; i--) {
if (!set.contains(s.substring(0, i))) {
judge = false;
break;
}
}
if (judge)
res = s;
}
}
return res;
}

[leetcode]720. Longest Word in Dictionary字典中最长的单词的更多相关文章

  1. Leetcode720.Longest Word in Dictionary词典中最长的单词

    给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最小的单词. 若无答案,则返回 ...

  2. [LeetCode] 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】Longest Word in Dictionary through Deleting 解题报告

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

  6. 【Leetcode_easy】720. Longest Word in Dictionary

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

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

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

  8. leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词

    一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...

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

随机推荐

  1. C++/Java小白解Leetcode题,发现了知识盲区……

    一.初见LeetCode 大一时候学习C++,根据课程一直在PTA平台做题目,数据结构和算法的作业题目也是在PTA.后来发现牛客网学习资源也很丰富,孤陋寡闻,前几个月在知道LeetCode这个平台,跟 ...

  2. LeetCode 030 Substring with Concatenation of All Words

    题目要求:Substring with Concatenation of All Words You are given a string, S, and a list of words, L, th ...

  3. Beta冲刺随笔——Day_Seven

    这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...

  4. 浅尝 Elastic Stack (三) Logstash + Beats

    本文使用 Filebeat,如果没有安装需要安装: curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat- ...

  5. day2(APlview+Serializers使用)

    1.APIview使用 ModelVIewSet 是对 APIView 封装  ModelSerializer是对Serializeer 1.1 在user/urls.py中添加路由 urlpatte ...

  6. Jdk源码-集合类主要原理和解析

    写在前面 熟悉Jdk原理的重要性不言而喻,作为Java开发者或者面试者,了解其实现原理也显得更为装逼,在Java读书计划我写到了,它是面试中最基础的一部分,所以单独拿出来做个总结,为了更好滴理解和学习 ...

  7. PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    注:网上搜来的快照,暂未验证 在java代码中请求https链接的时候,可能会报下面这个错误javax.net.ssl.SSLHandshakeException: sun.security.vali ...

  8. PyQt(Python+Qt)学习随笔:QTableWidget的获取指定位置项的item和itemAt方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 1.获取指定行和列的项 根据行和列可以获取对应位置的项,调用语法如下: QTableWidgetIt ...

  9. tensorflow GPU的使用

    参考:https://blog.csdn.net/mzpmzk/article/details/78647711 import os # 默认情况,TF 会占用所有 GPU 的所有内存, 我们可以指定 ...

  10. THE BUG 队第一次团队项目作业

    队名: THE BUG 队 2.队员学号: 杨梓琦 3118005115(队长) 温海源,3118005109 陈杰才,3118005089 李华,3118005097 钟明康,3118005123 ...