您的位置:首页 > 编程语言 > Python开发

lintcode python 代码 133 最长单词

2017-06-15 11:08 399 查看
给一个词典,找出其中所有最长的单词。

思路:统计每个单词长度,输出最长的单词。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python