lintcode python 代码 133 最长单词

时间:2023-02-01 18:10:31

给一个词典,找出其中所有最长的单词。
思路:统计每个单词长度,输出最长的单词。

class Solution:
# @param dictionary: a list of strings
# @return: a list of strings
def longestWords(self, dictionary):
# write your code here
m = len(dictionary)
result = []
if m == 0:
return result
dictlen = len(dictionary[0])
result.append(dictionary[0])
for d in range(1, m):
tmp = dictionary[d]
if len(tmp) == dictlen:
result.append(tmp)
elif len(tmp) > dictlen:
result = []
dictlen = len(tmp)
result.append(tmp)
return result