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

Example

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的更多相关文章

  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. 2017-2018 第一学期201623班《程序设计与数据结构》-第7&8周作业问题总结

    一.作业内容 第7周作业 http://www.cnblogs.com/rocedu/p/7484252.html#WEEK07 第8周作业 http://www.cnblogs.com/rocedu ...

  2. Android的环境搭建

    尽管以前并没有接触过软件开发.但是,现在网络资源实在是太丰富了.所以网搜了一下,认为Android的环境搭建可分为以下五个步骤来完成.第一步:安装JDK:第二步:配置Windows上JDK的变量环境: ...

  3. Beta阶段冲刺-6

    一. 每日会议 1. 照片 2. 昨日完成工作 3. 今日完成工作 4. 工作中遇到的困难 杨晨露:各种问题,虽然都是开发上面的问题,但是都提出来就有点头大了. 戴志斌:对小程序公众号的开发不了解,因 ...

  4. ElasticSearch 2 (2) - Setup

    ElasticSearch 2.1.1 (2) - Setup Installation Elasticsearch can be started using: $ bin/elasticsearc ...

  5. Delphi7如何实现让Tedit显示文字垂直居中(上下居中)

    通过下面的组件,可以在输入文字的时候自动垂直居中 直接把下面代码保存到Unit1.pas即可------------------------------------------ unit Unit1; ...

  6. poj1064 Cable master

    Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...

  7. MT【192】又是绝对值函数

    (2018浙江新高考联盟2018第三次联考填空压轴题) 已知$f(x)=x^2+x-2$,若函数$g(x)=|f(x)|-f(x)-2mx-2m^2$有三个不同的零点,则实数$m$的取值范围是____ ...

  8. 【题解】 Luogu P1541 乌龟棋总结 (动态规划)

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...

  9. 普通平衡树Treap(含旋转)学习笔记

    浅谈普通平衡树Treap 平衡树,Treap=Tree+heap这是一个很形象的东西 我们要维护一棵树,它满足堆的性质和二叉查找树的性质(BST),这样的二叉树我们叫做平衡树 并且平衡树它的结构是接近 ...

  10. 学习5_STM32--外设通信方式

    就拿stm32的外设spi来说,通信方式主要有3种 > spi常规收发方式        (在轮询机制下通过判断缓冲区空与非空作为收发依据) > spi中断收发方式 (在中断机制下收发数据 ...