Given a dictionary, find all of the longest words in the dictionary.

Example

Given

  1. {
  2. "dog",
  3. "google",
  4. "facebook",
  5. "internationalization",
  6. "blabla"
  7. }

the longest words are(is) ["internationalization"].

Given

  1. {
  2. "like",
  3. "love",
  4. "hate",
  5. "yes"
  6. }

the longest words are ["like", "love", "hate"].

分析:

因为要保持所有的最长string,所以我们可以用ARRAYLIST。如果arraylist为空,直接加进去,否则我们得把新的字符串和arraylist里面的字符串进行比较。如果小于arraylist里面的字符串,do nothing, 如果相等,则加进去,如果大于,则清空arraylist,然后把该字符串加进去。

  1. class Solution {
  2. /**
  3. * @param dictionary: an array of strings
  4. * @return: an arraylist of strings
  5. */
  6. ArrayList<String> longestWords(String[] dictionary) {
  7. if (dictionary == null || dictionary.length == )
  8. return null;
  9. ArrayList<String> list = new ArrayList<String>();
  10.  
  11. for (String str : dictionary) {
  12. if (list.size() == ) {
  13. list.add(str);
  14. } else if (list.get().length() < str.length()) {
  15. list.clear();
  16. list.add(str);
  17. } else if (list.get().length() == str.length()) {
  18. list.add(str);
  19. }
  20. }
  21. return list;
  22. }
  23. }

Longest Words的更多相关文章

  1. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  2. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  3. [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 ...

  4. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  5. [LeetCode] Longest Repeating Character Replacement 最长重复字符置换

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  6. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  7. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  8. [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 ...

  9. [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 ...

  10. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

随机推荐

  1. Linux命令(十三) 建立目录 mkdir 删除目录 rmdir

    一.建立目录 mkdir 命令简介 mkdir 命令用于创建指定的目录.创建目录时当前用户对需要操作的目录有读取权限.如果目录已经存在,会提示报错并推出. mkdir 可以创建多级目录. 常用参数说明 ...

  2. c# 行转列

    将下面表(1)格式的数据转换为表(2)格式的数据.很明显,这是一个行转列的要求,本想在数据库中行转列,因为在数据库中行转列是比较简单的,方法可以参考本站SQLServer中(行列转换)行转列及列转行且 ...

  3. Strategic Game HDU - 1054(最小顶点覆盖)

    最小顶点覆盖:用最少的点,让每条边都至少和其中一个点关联: ...以为自己很聪明..用边连边...最后还是点连点  哎.... hc 写的  匈牙利足够///// #include <iostr ...

  4. 洛谷P3862 8月月赛B

    https://www.luogu.org/problemnew/show/P3862#sub P3862 8月月赛B 推公式:f(n)->f(n+1) 奇葩的预处理 https://www.l ...

  5. 样本服从正态分布,证明样本容量n乘样本方差与总体方差之比服从卡方分布x^2(n)

    样本服从正态分布,证明样本容量n乘样本方差与总体方差之比服从卡方分布x^2(n) 正态分布的n阶中心矩参见: http://www.doc88.com/p-334742692198.html

  6. 【疑点】js中的break,continue和return到底怎么用?

    转: [疑点]js中的break,continue和return到底怎么用? 为什么要说个?好像很简单,但是我也会迷糊,不懂有时候为什么要用return,然而break和continue也经常和他放在 ...

  7. 使用Sql语句快速将数据表转换成实体类

    开发过程中经常需要根据数据表编写对应的实体类,下面是使用sql语句快速将数据表转换成对应实体类的代码,使用时只需要将第一行'TableName'引号里面的字母换成具体的表名称就行了: declare ...

  8. 网络获取json数据并解析

    1.升级流程分析

  9. python学习笔记6--mockserver

    一.mockserver的应用 有时候测试我们需要调用一些三方接口或者未开发完成的接口,完成我们的业务流程测试,但是这时候可能我们只知道接口返回值,接口并没有完全开发完成或可以让我们任意调用,这时候就 ...

  10. H5 Day1 练习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...