[leetcode]720. Longest Word in Dictionary字典中最长的单词
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字典中最长的单词的更多相关文章
- Leetcode720.Longest Word in Dictionary词典中最长的单词
给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最小的单词. 若无答案,则返回 ...
- [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 (字典里最长的单词)
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】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】720. Longest Word in Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...
- leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词
一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...
- [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 039 Combination Sum
题目要求:Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique c ...
- Beta冲刺随笔——Day_Seven
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...
- Fiddler 4 对app接口抓取
一.先打开模拟器 二.在Fiddler 4 选项中修改端口号和去掉一个勾选 三.在终端查看ip 输入ipconfig 四.点开模拟器的设置 五.点击WLAN 六.长按网络,修改网络 七.输入ip端口号 ...
- jdk版本下载
oracleJDK oracle各版本下载地址:https://www.oracle.com/technetwork/java/archive-139210.html openJDK 编译好的 ojd ...
- 区块链学习7:超级账本项目Fabric中的背书、背书节点、背书策略、背书签名
☞ ░ 前往老猿Python博文目录 ░ 在Hyperledger Fabric区块链中,有背书节点进行背书,Hyperledger Fabric 使用背书策略来定义哪些节点需要执行交易. Hyper ...
- moviepy音视频剪辑:TextClip.list(font)和search搜索字体报错UnicodeDecodeError:utf-8 codec cannott decode byte 问题
☞ ░ 前往老猿Python博文目录 ░ 在moviepy2.0.0.Dev版本中,执行如下语句: from moviepy.editor import * TextClip.search('gb', ...
- 关于moviepy打包报错AttributeError: module audio/video.fx.all has no attribute fadein、crop文章的纠错和抄袭
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 老猿前面有篇文章<moviepy应用pyin ...
- anaconda 配置虚拟环境
工作时有时候会遇到不同版本的问题,比如深度学习tensorflow 1.14版本 和 2.0版本,或者cpu版本和gpu版本,那么这个时候建立虚拟环境就很方便了 anaconda命令行下 1) act ...
- .NET Core集成SkyWalking+SkyAPM-dotne实现分布式链路追踪
.NET Core集成SkyWalking+SkyAPM-dotnet实现分布式链路追踪 SkyWalking是一款APM(应用性能管理),其他的还有Cat.Zipkin.Pinpoint等. 随着微 ...
- cglib、orika、spring等bean copy工具性能测试和原理分析
简介 在实际项目中,考虑到不同的数据使用者,我们经常要处理 VO.DTO.Entity.DO 等对象的转换,如果手动编写 setter/getter 方法一个个赋值,将非常繁琐且难维护.通常情况下,这 ...