博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
692. Top K Frequent Words(python+cpp)(字典树统计)
阅读量:3702 次
发布时间:2019-05-21

本文共 3837 字,大约阅读时间需要 12 分钟。

题目:

Given a non-empty list of words, return the k most frequent elements.

Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.
Example 1:

Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 Output: ["i", "love"] Explanation: "i" and "love" are the two most frequent words.Note that "i" comes  before "love" due to a lower alphabetical order.

Example 2:

Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 Output: ["the", "is", "sunny", "day"] Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.

Note:

You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Input words contain only lowercase letters.
Follow up:
Try to solve it in O(n log k) time and O(n) extra space.

解释:

直接用Counter()后排序就能做,这里主要探讨用Trie做词频统计的方法。
统计+排序的解法:
python代码:

from collections import Counterclass Solution(object):    def topKFrequent(self, words, k):        """        :type words: List[str]        :type k: int        :rtype: List[str]        """        c=Counter(words)        keys=c.keys();        keys.sort(key = lambda w: (-c[w], w))        return keys[:k]

c++代码:

#includeusing namespace std;class Solution {
public: vector
topKFrequent(vector
& words, int k) {
map
_count; for (auto word:words) _count[word]++; vector
> list_count(_count.begin(),_count.end()); sort(list_count.begin(),list_count.end(),cmp); vector
result; for(int i=0;i
& item1,const pair
& item2) { if (item1.second!=item2.second) return item1.second>item2.second; else return item1.first

字典树解法(字典树统计词频),字典树统计词频还是需要用字典存起来之后排序的,但是用字典树有利于压缩存储空间,达到 follow up 所要求的时间复杂度和空间复杂度:

python代码(比用Counter()慢):

#用字典树还是需要排序??用字典树统计词频有利于压缩空间#不知道Counter()内部是咋实现的,没准就是字典树呢from collections import defaultdictclass TrieNode(object):    def __init__(self):        self.children=defaultdict(TrieNode)        self.is_word=False        self.freq=0class Solution(object):    def topKFrequent(self, words, k):        """        :type words: List[str]        :type k: int        :rtype: List[str]        """        def insert(root,word):            current=root            for letter in word:                current=current.children[letter]            #在最后一个字母的位置,is_word变为True            current.is_word=True            current.freq+=1            return current.freq        root = TrieNode()        _dict={
} for word in words: freq=insert(root,word) _dict[word]=freq keys=_dict.keys(); keys.sort(key = lambda w: (-_dict[w], w)) return keys[:k]

c++代码:

#includeusing namespace std;struct TrieNode {
bool isWord; int freq; char c; map
children; TrieNode(char x) : c(x),isWord(false),freq(0){
} TrieNode() : isWord(false),freq(0){
}};class Solution {
public: vector
topKFrequent(vector
& words, int k) {
TrieNode* root= new TrieNode(); map
_count; for (auto word:words) {
int freq=insert(root,word); _count[word]=freq; } vector
> list_count(_count.begin(),_count.end()); sort(list_count.begin(),list_count.end(),cmp); vector
result; for(int i=0;i
children.count(letter)) current->children[letter]=new TrieNode(letter); current=current->children[letter]; } current->isWord=true; current->freq++; return current->freq; } static bool cmp(const pair
& item1,const pair
& item2) { if (item1.second!=item2.second) return item1.second>item2.second; else return item1.first

总结:

虽然字典树的解法速度慢,但是学会了用字典树统计词频,况且节省了存储空间。

转载地址:http://bzmcn.baihongyu.com/

你可能感兴趣的文章
# java学习笔记 2020 1/17(二)慕课网 注释
查看>>
# java学习笔记 2020 1/19(四)慕课网 switch & 循环语句
查看>>
# java学习笔记 2020 2/7(九)慕课网 使用方法
查看>>
入坑Golang —— 数据类型的基本介绍
查看>>
活动选择问题
查看>>
懒虫小鑫
查看>>
最少拦截系统
查看>>
魔趣9上手体验(更新药丸版)(坚果pro2)
查看>>
原生谷歌人脸解锁启用
查看>>
递归的函数
查看>>
上升子序列
查看>>
鬼吹灯之龙岭迷窟
查看>>
坚果pro2刷MIUI10
查看>>
坚果pro2救砖(文末包含900E的解决方法)
查看>>
setTimeout和setInterval执行时间问题
查看>>
mouse冒泡事件
查看>>
java NIO(二)----直接缓冲区和非直接缓冲区
查看>>
java NIO(三)----通道(Channel)
查看>>
java NIO(四)----阻塞IO与非阻塞IO
查看>>
java实现生成二维码
查看>>