字典树

6-06 2,446 views

字典树,又名Trier树,可以用于单词的查找和统计。
字典树的示例如下图所示,其中,若根节点为第0层,则第k层节点表示字典中单词前k个字符,从根节点至树中标黄节点的路径表示以该节点字符为末尾的单词,例如,too、tooth、tea、two等。
172655272
字典树中的节点可用以下结构体表示:

字典树的初始化和插入单词操作:

字典树可用于计算某个文本中出现次数最多的前k个单词:遍历文本中的单词,将每个单词插入到字典树中,重复的单词实际不再添加,而是将原有单词末尾节点中的count加1,最后,字典树中根节点到所有endFlag为true的节点的路径为文本中出现的所有单词,节点中的count值为这些单词出现的次数,通过排序可以得到出现次数最多的前k个单词。

Move Zeroes

Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. For exa...

阅读全文

Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not ...

阅读全文

Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space...

阅读全文