今天学习到了Trie树
字典树的详细定义可以看
Stackoverflow上的简洁实践
How to create a trie in Python
class Trie:
def __init__(self):
self._end = '_end_'
def make_trie(self,*words):
root = dict()
for word in words:
current_dict = root
for letter in word:
current_dict = current_dict.setdefault(letter,{})
current_dict[self._end] = self._end
return root
if __name__ == '__main__':
trie = Trie()
print(trie.make_trie('foo', 'bar', 'baz', 'barz'))
控制台输出
{'f': {'o': {'o': {'_end_': '_end_'}}},
'b': {'a': {'r': {'_end_': '_end_', 'z': {'_end_': '_end_'}}, 'z': {'_end_': '_end_'}}}}
在trie树中查找单词
class Trie:
def __init__(self):
self._end = '_end_'
def make_trie(self,*words):
root = dict()
for word in words:
current_dict = root
for letter in word:
current_dict = current_dict.setdefault(letter,{})
current_dict[self._end] = self._end
return root
def in_trie(self,trie,word):
current_dict = trie
for letter in word:
if letter not in current_dict:
return False
current_dict = current_dict[letter]
return self._end in current_dict
if __name__ == '__main__':
trie = Trie()
print(trie.make_trie('foo', 'bar', 'baz', 'barz'))
print(trie.in_trie(trie.make_trie('foo', 'bar', 'baz', 'barz'),'bar'))
# output: True
单词的压缩编码(leetcode)
给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A。
例如,如果这个列表是 [“time”, “me”, “bell”],我们就可以将其表示为 S = “time#bell#” 和
indexes = [0, 2, 5]。对于每一个索引,我们可以通过从字符串 S 中索引的位置开始读取字符串,直到 “#” 结束,来恢复我们之前的单词列表。
那么成功对给定单词列表进行编码的最小字符串长度是多少呢?
示例:
输入: words = [“time”, “me”, “bell”] 输出: 10 说明: S = “time#bell#” ,
indexes = [0, 2, 5] 。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/short-encoding-of-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
我觉得leetcode的官方实现才是最6的,pythonic极致
class Solution:
def minimumLengthEncoding(self, words: List[str]) -> int:
words = list(set(words)) #去重
# Trie是带有已创建节点的嵌套字典
# 当其中缺少节点时会创建节点
Trie = lambda: collections.defaultdict(Trie)
trie = Trie()
#reduce(..., S, trie) is trie[S[0]][S[1]][S[2]][...][S[S.length - 1]],将单词反序插入
nodes = [reduce(dict.__getitem__, word[::-1], trie)
for word in words]
#如果节点没有邻居节点,则添加该单词
return sum(len(word) + 1
for i, word in enumerate(words)
if len(nodes[i]) == 0)
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/short-encoding-of-words/solution/dan-ci-de-ya-suo-bian-ma-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
奇怪的知识又增加了哈哈哈~
今早还学习到了
set.discard(ele)# 可以移除集合中不存在的元素