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;
    }
};
工作小总结,有错请指出,谢谢。
原文地址:https://www.cnblogs.com/zilanghuo/p/5302865.html