LintCode Q133 Longest Words in Python

  • Jinhai ZHOU
  • 2 Minutes
  • 2017年2月9日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
# @param dictionary: a list of strings
# @return: a list of strings
def longestWords(self, dictionary):
# write your code here
# solve the problem only one pass
longests = []
for string in dictionary:
if len(longests) == 0:
longests.append(string)
else:
if len(string) > len(longests[-1]):
longests = []
longests.append(string)
elif len(string) == len(longests[-1]):
longests.append(string)
return longests
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。