Leetcode 720. Longest Word in Dictionary

Brute Force(暴力)查找即可.

注意Python的元组也是可以比大小的,就是先比第一个元素,再第二个...

所以排序时key先按len排,len一样再按字典序排:就是key返回元组.

class Solution(object):
    def longestWord(self, words):
        words.sort(key=lambda x: (-len(x), x))
        words_set = set(words)
        for w in words:
            if all(w[:i] in words_set for i in range(1, len(w))):
                return w
        return ''
原文地址:https://www.cnblogs.com/zywscq/p/10705671.html