成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Longest Words

roadtogeek / 3144人閱讀

Problem

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"].

Challenge

It"s easy to solve it in two passes, can you do it in one pass?

Solution
class Solution {
    ArrayList longestWords(String[] dictionary) {
        // write your code here
        ArrayList res = new ArrayList();
        for (String word: dictionary) {
            if (res.isEmpty() || res.get(0).length() < word.length()) {
                res.clear();
                res.add(word);
            }
            else if (res.get(0).length() == word.length()) res.add(word);
            else continue;
        }
        return res;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://systransis.cn/yun/65506.html

相關文章

  • [LintCode/LeetCode] Longest Palindrome Substring

    摘要:是左閉右開區(qū)間,所以要。,要理解是和之間只有一個元素。循環(huán)每次的時候,都要更新子串更大的情況。補一種中點延展的方法循環(huán)字符串的每個字符,以該字符為中心,若兩邊為回文,則向兩邊繼續(xù)延展。循環(huán)返回長度最長的回文串即可。 Problem Given a string S, find the longest palindromic substring in S. You may assume ...

    AaronYuan 評論0 收藏0
  • [LintCode] Longest Substring Without Repeating Cha

    摘要:哈希表法雙指針法只有當也就是時上面的循環(huán)才會結束,,跳過這個之前的重復字符再將置為 Problem Given a string, find the length of the longest substring without repeating characters. Example For example, the longest substring without repeat...

    Scliang 評論0 收藏0
  • [LintCode] Longest Palindrome

    Problem Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example Aa is not ...

    nicercode 評論0 收藏0
  • [LintCode] Longest Increasing Subsequence

    Problem Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Clarification Whats the definition of longest increasing subsequence?...

    Flands 評論0 收藏0
  • [LintCode] Longest Repeating Subsequence

    摘要:用二維數(shù)組進行動態(tài)規(guī)劃,作為第位和的位已有的重復子序列長度最大值計數(shù)器。 Problem Given a string, find length of the longest repeating subsequence such that the two subsequence don’t have same string character at same position, i.e...

    Karuru 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<