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, ...
随机推荐
- Linux命令(十三) 建立目录 mkdir 删除目录 rmdir
一.建立目录 mkdir 命令简介 mkdir 命令用于创建指定的目录.创建目录时当前用户对需要操作的目录有读取权限.如果目录已经存在,会提示报错并推出. mkdir 可以创建多级目录. 常用参数说明 ...
- c# 行转列
将下面表(1)格式的数据转换为表(2)格式的数据.很明显,这是一个行转列的要求,本想在数据库中行转列,因为在数据库中行转列是比较简单的,方法可以参考本站SQLServer中(行列转换)行转列及列转行且 ...
- Strategic Game HDU - 1054(最小顶点覆盖)
最小顶点覆盖:用最少的点,让每条边都至少和其中一个点关联: ...以为自己很聪明..用边连边...最后还是点连点 哎.... hc 写的 匈牙利足够///// #include <iostr ...
- 洛谷P3862 8月月赛B
https://www.luogu.org/problemnew/show/P3862#sub P3862 8月月赛B 推公式:f(n)->f(n+1) 奇葩的预处理 https://www.l ...
- 样本服从正态分布,证明样本容量n乘样本方差与总体方差之比服从卡方分布x^2(n)
样本服从正态分布,证明样本容量n乘样本方差与总体方差之比服从卡方分布x^2(n) 正态分布的n阶中心矩参见: http://www.doc88.com/p-334742692198.html
- 【疑点】js中的break,continue和return到底怎么用?
转: [疑点]js中的break,continue和return到底怎么用? 为什么要说个?好像很简单,但是我也会迷糊,不懂有时候为什么要用return,然而break和continue也经常和他放在 ...
- 使用Sql语句快速将数据表转换成实体类
开发过程中经常需要根据数据表编写对应的实体类,下面是使用sql语句快速将数据表转换成对应实体类的代码,使用时只需要将第一行'TableName'引号里面的字母换成具体的表名称就行了: declare ...
- 网络获取json数据并解析
1.升级流程分析
- python学习笔记6--mockserver
一.mockserver的应用 有时候测试我们需要调用一些三方接口或者未开发完成的接口,完成我们的业务流程测试,但是这时候可能我们只知道接口返回值,接口并没有完全开发完成或可以让我们任意调用,这时候就 ...
- H5 Day1 练习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...