[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 ...
随机推荐
- Android多触点总结
文章部分内容参考: http://blog.csdn.net/barryhappy/article/details/7392326 总结: 1. event.getX()可以得到x的坐标,里面的参数0 ...
- java集合源码分析(三):ArrayList
概述 在前文:java集合源码分析(二):List与AbstractList 和 java集合源码分析(一):Collection 与 AbstractCollection 中,我们大致了解了从 Co ...
- JVM(四)-虚拟机对象
概述: 上一篇文章,介绍了虚拟机类加载的过程,那么类加载好之后,虚拟机下一步该干什么呢.我们知道java是面向对象的编程语言,所以对象可以说是java'的灵魂,这篇文章我们就来介绍 虚拟机是如何创建对 ...
- 第14.1节 通过Python爬取网页的学习步骤
如果要从一个互联网前端开发的小白,学习爬虫开发,结合自己的经验老猿认为爬虫学习之路应该是这样的: 一. 了解HTML语言及css知识 这方面的知识请大家通过w3school 去学习,老猿对于html总 ...
- kettle如何从cube抽数据
接触kettle已经还是有一段时间了,但是一直都使用简单的输入.输出(二维数据库to二维数据库).今天,突然接到一个需求,需要从多维数据库(CUBE)里面将数据抽取到二维数据库,我难住了,不知道该如何 ...
- rsync 参数说明及使用参数笔记
第1章 rsync 命令简介 rsync 是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据镜像同步备份的优秀工具. 1.1.1 语法格式 三种模式: 1)本地模式 rsync [选项] ...
- socket ThreadingTCPServer学习笔记
文件上传#服务端 while True: conn,address = sk.accept() conn.sendall(bytes('欢迎你小sb',encoding='utf-8')) str_s ...
- SpringBoot+Redis相关配置文件
springboot整合redis配置类 package com.yalong.config; import com.fasterxml.jackson.annotation.JsonAutoDete ...
- SpringBoot魔法堂:应用热部署实践与原理浅析
前言 后端开发的同学想必每天都在重复经历着修改代码.执行代码编译,等待--重启Tomcat服务,等待--最后测试发现还是有bug,然后上述流程再来一遍(我听不见)
- 学习笔记——JS语言精粹
JS作用域是基于词法作用域的顶级对象. JS是一门弱类型语言,强类型能在编译时检测错误. JS是唯一一门所有浏览器都能识别的语言. 块注释对于被注释的代码是不安全的,例如/* var rm=/a*/ ...