720. Longest Word in Dictionary

题目

720. Longest Word in Dictionary(不是很懂)
题意比较模糊。

优秀代码

class Solution:
    def longestWord(self, words: 'List[str]') -> 'str':
        res = None
        maxlen = 0
        look_up = set(words)
        for word in words:
            if len(word) >= maxlen:
                if len(word) == maxlen: #相等
                    if word < res:
                        if all(word[:i] in look_up for i in range(1, len(word))):
                            res = word
                else:
                    if all(word[:i] in look_up for i in range(1, len(word))):
                        res = word
                        maxlen = len(word)
        return res
class Solution(object):
    def longestWord(self, words):
        words.sort()
        words_set, longest_word = set(['']), ''
        for word in words:
            if word[:-1] in words_set:
                words_set.add(word)
                if len(word) > len(longest_word):
                    longest_word = word
        return longest_word

相关文章:

  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2022-12-23
  • 2021-10-21
  • 2022-02-28
  • 2021-12-25
猜你喜欢
  • 2022-12-23
  • 2021-05-16
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2021-06-01
相关资源
相似解决方案