[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 ...
随机推荐
- PyQt(Python+Qt)学习随笔:QAbstractItemView的editTriggers属性以及平台编辑键(platform edit key )
老猿Python博文目录 老猿Python博客地址 editTriggers属性 editTriggers属性用于确认哪些用户操作行为会触发ItemView中的数据项进入编辑模式. 此属性是由枚举类E ...
- 常见SQL注入点判断
sql注入手工检测 SQL注入手工检测 1基本检测 数字型 字符型 搜索型 POST注入 布尔盲注 报错注入 堆叠注入 判断是什么数据库 2绕过技巧 大小写 替换关键字 使用编码 注释和符号 等价函数 ...
- .NET 面试题汇总(带答案)
1.维护数据库的完整性.一致性.你喜欢用触发器还是自写业务逻辑?为什么? 答:尽可能用约束(包括CHECK.主键.唯一键.外键.非空字段)实现,这种方式的效率最好:其次用触发器,这种方式可以保证无论何 ...
- C# Email 帮助类 EmailHelper
1. 配置文件 App.config <?xml version="1.0" encoding="utf-8" ?> <configurati ...
- new一个对象时,会经历哪些步骤
(1)创建一个对象:(2)将构造函数的作用域赋值给新对象(因此this就指向了这个新对象):(3)执行构造函数中的代码(为这个新对象添加属性):(4)返回新对象
- Markdown常用数学符号&公式
符号 代码 描述 \(\sim\) $\sim$ 波浪号 \(\sum\) $\sum$ 求和公式 \(\sum_{i=0}^n\) $\sum_{i=0}^n$ 求和上下标 \(\times\) $ ...
- 题解-[SDOI2014]数数
[SDOI2014]数数 这题的前置知识是AC自动机和dp,前置题目是 [JSOI2007]文本生成器,前置题目我写的题解 题解-[JSOI2007]文本生成器.我的讲解假设你做过上面那道题. 这题比 ...
- block、inline、inline-block区别以及标签嵌套
1.block 将元素转为块元素,块元素占一行,可以设置宽和高. 2.inline 将元素转为行内元素,占一行,不可以设置宽和高. 3.inline-block 将元素设置为行内块元素,这时元素既可以 ...
- Angular:自定义属性指令
①在命令行窗口下用 CLI 命令ng g directive创建指令类文件 ②将directives/light.directive.ts文件改造一番 import { Directive, Elem ...
- STL—— 容器(vector)的内存分配,声明时的普通构造&带参构造
vector 的几种带参构造 & 初始化与内存分配: 1. 普通的带参构造: vector 的相关对象可以在声明时通过 vector 的带参构造函数进行内存分配,如下: 1 #include ...