leetcode:longest words

Posted 自朗活

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode:longest words相关的知识,希望对你有一定的参考价值。

1、

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

Given

{
  "dog",
  "google",
  "facebook",
  "internationalization",
  "blabla"
}

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

2、

  思路:

  1、得到数组里面最长的字符串大小

  2、判断相等的字符串大小,添加进去。

3、源码:

  

class Solution {
    /**
     * @param dictionary: an array of strings
     * @return: an arraylist of strings
     */
    ArrayList<String> longestWords(String[] dictionary) {
        // write your code here
        int maxLen = 0;
        ArrayList<String> ans = new ArrayList<String>();
        for (int i=0; i<dictionary.length; ++i) 
            if (dictionary[i].length()>maxLen) maxLen = dictionary[i].length();
        for (int i=0; i<dictionary.length; ++i)
            if (dictionary[i].length()==maxLen) ans.add(dictionary[i]);
        return ans;
    }
};

 

以上是关于leetcode:longest words的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] Longest Word in Dictionary 字典中的最长单词

[LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词

Leetcode-- Longest Palindromic Substring

LeetCode - Longest Substring Without Repeating Characters

LeetCode Longest Palindrome

leetcode:longest-increasing