前k个高频单词
问题描述
给一非空的单词列表,返回前 k 个出现次数最多的单词。
返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。
示例 1:
输入: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
输出: ["i", "love"]
解析: "i" 和 "love" 为出现次数最多的两个单词,均为2次。
注意,按字母顺序 "i" 在 "love" 之前。
示例 2:
输入: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
输出: ["the", "is", "sunny", "day"]
解析: "the", "is", "sunny" 和 "day" 是出现次数最多的四个单词,
出现次数依次为 4, 3, 2 和 1 次。
注意:
- 假定 k 总为有效值, 1 ≤ k ≤ 集合元素数。
- 输入的单词均由小写字母组成。
题目分析
这道题需要两大版块,引入映射Map<key,value>,存储String(单词)和Integer(次数)一一对应关系
- 首先,按照<key>进行排序
- 再按照字母进行排序
核心框架
public List<String> topKFrequent(String[] words, int k) {
/*三步骤,提取<key>属性,放在数组内,排序
Map<String, Integer> wordToCount = count(words);//count方法实现map的一一对应
Map<Integer, List<String>> countToWordList = remap(wordToCount);//remap实现wordToCount<String,Integer>反转<Integer,List<String>>,countToWordList实现一对多
*/
Set<Integer> keys = countToWordList.keySet();
int[] counts = new int[keys.size()];
int i = 0;
for (int key : keys) {
counts[i++] = key;
}
Arrays.sort(counts);
//三步骤,while循环已录取数<录取数 相等时跳出
//counts数组从后向前找key层value进行排序(字母)
//如果这一层个数大于k-j街区响应字段;小于等于放入所有
List<String> result = new ArrayList<>();
int j = 0;
int index = counts.length - 1;
Comparator<String> comparator = new StringComparator();
while (j < k) {
int c = counts[index--];
List<String> wordList = countToWordList.get(c);
wordList.sort(comparator);
if (wordList.size() <= k - j) {
result.addAll(wordList);
j += wordList.size();
} else {
result.addAll(wordList.subList(0, k - j));
j = k;
}
}
return result;
}
完整实现
import java.util.*;
public class P5 {
public static class StringComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
}
Map<String, Integer> count(String[] words) {
Map<String, Integer> wordToCount = new HashMap<>();
for (String word : words) {
int c = wordToCount.getOrDefault(word, 0);
wordToCount.put(word, c + 1);
}
return wordToCount;
}
Map<Integer, List<String>> remap(Map<String, Integer> wordToCount) {
Map<Integer, List<String>> countToWordList = new HashMap<>();
for (Map.Entry<String, Integer> e : wordToCount.entrySet()) {
String word = e.getKey();
int count = e.getValue();
List<String> wordList = countToWordList.get(count);
if (wordList == null) {
wordList = new ArrayList<>();
countToWordList.put(count, wordList);
}
wordList.add(word);
}
return countToWordList;
}
public List<String> topKFrequent(String[] words, int k) {
Map<String, Integer> wordToCount = count(words);
Map<Integer, List<String>> countToWordList = remap(wordToCount);
Set<Integer> keys = countToWordList.keySet();
int[] counts = new int[keys.size()];
int i = 0;
for (int key : keys) {
counts[i++] = key;
}
Arrays.sort(counts);
List<String> result = new ArrayList<>();
int j = 0;
int index = counts.length - 1;
Comparator<String> comparator = new StringComparator();
while (j < k) {
int c = counts[index--];
List<String> wordList = countToWordList.get(c);
wordList.sort(comparator);
if (wordList.size() <= k - j) {
result.addAll(wordList);
j += wordList.size();
} else {
result.addAll(wordList.subList(0, k - j));
j = k;
}
}
return result;
}
public static void main(String[] args) {
String[] words = {
"i", "love", "leetcode",
"i", "love", "coding"
};
List<String> r = new P5().topKFrequent(words, 3);
System.out.println(r);
}
}