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;
    }
};

 

posted @ 2016-03-21 17:36  自朗活  阅读(137)  评论(0编辑  收藏  举报