133 · 最长单词


描述
给一个词典,找出其中所有最长的单词。

样例
样例 1:
输入: {
"dog",
"google",
"facebook",
"internationalization",
"blabla"
}
输出: ["internationalization"]

样例 2:
输入: {
"like",
"love",
"hate",
"yes"
}
输出: ["like", "love", "hate"]

挑战
遍历两次的办法很容易想到,如果只遍历一次你有没有什么好办法?

class Solution:
    """
    @param: dictionary: an array of strings
    @return: an arraylist of strings
    """
    def longestWords(self, dictionary):
        l = max([len(i) for i in dictionary])
        return [i for i in dictionary if len(i) == l]