Longest Words
Given a dictionary, find all of the longest words in the dictionary.
Given
{
"dog",
"google",
"facebook",
"internationalization",
"blabla"
}
the longest words are(is) ["internationalization"]
.
Given
{
"like",
"love",
"hate",
"yes"
}
the longest words are ["like", "love", "hate"]
.
分析:
因为要保持所有的最长string,所以我们可以用ARRAYLIST。如果arraylist为空,直接加进去,否则我们得把新的字符串和arraylist里面的字符串进行比较。如果小于arraylist里面的字符串,do nothing, 如果相等,则加进去,如果大于,则清空arraylist,然后把该字符串加进去。
class Solution {
/**
* @param dictionary: an array of strings
* @return: an arraylist of strings
*/
ArrayList<String> longestWords(String[] dictionary) {
if (dictionary == null || dictionary.length == )
return null;
ArrayList<String> list = new ArrayList<String>(); for (String str : dictionary) {
if (list.size() == ) {
list.add(str);
} else if (list.get().length() < str.length()) {
list.clear();
list.add(str);
} else if (list.get().length() == str.length()) {
list.add(str);
}
}
return list;
}
}
Longest Words的更多相关文章
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Longest Repeating Character Replacement 最长重复字符置换
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- (Alpha)Let's-NABC
效果图 NABC Need 在日常生活中我们也许经常有这样的疑问:在空闲时间内经常感到无处可去:抑或是有个心血来潮的想法却无人响应:或者困扰于自己狭小的交际圈,希望能有更多的朋友.(打个羽毛球没人一起 ...
- 重温redis命令
redis是已知的性能最快的key-value 数据库. 1.key相关命令 exists key :检查指定的key是否存在 1表示存在 0表示不存在 del key1,key2,key3....: ...
- 将ssh失败的用户放入hosts.deny中
1.find / -name secure 找到linux系统安全日志文件 2.cp `find / -name secure` /tmp/`date +%F` 将secure文件复制出来 或者使用f ...
- 小程序开发 event对象中 target和currentTarget 属性的区别。
首先本质区别是: event.target返回触发事件的元素 event.currentTarget返回绑定事件的元素 p包含在div内 在outer上点击时,target跟currentTarget ...
- Windows 64位环境的Java 服务配置
有个任务,需要远程起调Windows64服务器下的程序,那么需要在Windows服务器中注入一个deamon服务,都知道Linux环境做成后台服务非常简单,nohup &很快能解决问题,但wi ...
- BZOJ4078 WF2014Metal Processing Plant(二分答案+2-SAT)
题面甚至没给范围,由数据可得n<=200.容易想到二分答案,暴力枚举某集合的价值,2-SATcheck一下即可.这样是O(n4logn)的. 2-SAT复杂度已经是下界,考虑如何优化枚举.稍微改 ...
- CF375D Tree and Queries
题意翻译 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. 感谢@elijahqi 提供的翻译 ...
- 【题解】 [HNOI2002]营业额统计 (Splay)
懒得复制,戳我戳我 Solution: \(Splay\)板子题,注意可以选择相等大小 Code: //It is coded by Ning_Mew on 4.10 #include<bits ...
- 【bzoj2001】 Hnoi2010—City 城市建设
http://www.lydsy.com/JudgeOnline/problem.php?id=2001 (题目链接) 题意 给出一张无向图,$m$组操作,每次修改一条边的权值,对于每次操作输出修改之 ...
- 前端学习 -- Css -- 定义列表
定义列表用来对一些词汇或内容进行定义 使用dl来创建一个定义列表 dl中有两个子标签 dt : 被定义的内容 dd : 对定义内容的描述 同样dl和ul和ol之间都可以互相嵌套 <!DOCTYP ...