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. 天池新闻推荐比赛1:赛题理解+baseline

    天池新闻推荐比赛1:赛题理解+baseline 一.比赛信息 比赛链接: ​ https://tianchi.aliyun.com/competition/entrance/531842/inform ...

  2. LeetCode 032 Longest Valid Parentheses

    题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...

  3. 在 Spring Boot 中使用 Flyway

    一.Flyway 介绍 Flyway 是一个开源的数据库迁移工具,MySQL, SQL Server, Oracle 等二十多种数据库 在 Flyway 中数据库的所有改变均称为迁移(migratio ...

  4. vue回调接口

    1.微博回调接口 1.1oauth/urls.py 中添加路由 urlpatterns = [ path('weibo/callback/', views.OauthWeiboCallback.as_ ...

  5. go语言小知识点

    fmt包相关 fmt包主要是实现了格式化的I/O函数 fmt.Println()   行打印 fmt.Printf() 格式化输出 %d int %s str %t type,类型 %p 内存地址 % ...

  6. 三分法-洛谷P3382

    wampserver没下下来,lcm莫比乌斯反演写挂了,splay树段错误, nobody ever knows writing bugs better than me 然后今晚要打cf,不如先写个三 ...

  7. 老猿学5G:融合计费场景的Nchf_ConvergedCharging_Create、Update和Release融合计费消息交互过程

    ☞ ░ 前往老猿Python博文目录 ░ 一.Nchf_ConvergedCharging_Create交互过程 Nchf_ConvergedCharging_Create 服务为CTF向CHF请求提 ...

  8. 转2:Python字符编码详解

    1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Information Interchange),是一种单字节的编码.计算机世界里一开始只有 ...

  9. PyQt(Python+Qt)学习随笔:树型部件QTreeWidget中的topLevelItem、indexOfTopLevelItem和takeTopLevelItem方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 获取指定位置顶层项 树型部件QTreeWidget中,通过topLevelItem方法根据位置索引取 ...

  10. Python正则表达式re.match(r"(..)+", "a1b2c3")匹配结果为什么是”c3”?

    在才开始学习正则表达式处理时,老猿对正则表达式:re.match(r"(-)+", "a1b2c3") 返回的匹配结果为"c3"没有理解,学 ...