我有一个HashMap<&str,i32>,用来存储不同单词的频率。我必须返回最频繁的字符串。在使用Java之前,我已经完成了这个问题,但是我无法想出如何在生锈中完成这个问题。
//my hashmap
Map<String, Integer> map = new HashMap<>();
//my priority queue
PriorityQueue<Map.Entry<String, Integer>>pq = new PriorityQueue<>(
(a,b) -> a.getValue() == b.getValue() ? b.getKey().compareTo(a.getKey()) :a.getValue() - b.getValue()
);
我创建了一个PriorityQueue,并通过了我所需要的比较器,我不知道如何在生锈时做同样的事情。
地图{“你好”:2,“世界”:3,“程序”:1}
我希望BinaryHeap看起来像这样:堆{“世界”,“你好”,“程序”};
发布于 2022-07-17 14:23:43
通常,比较函数(Ord
impl)不可能引用它不包含的数据,参见这个问题。在这种情况下,由于不希望在插入后更改密钥的优先级,所以可以插入(count, T)
的元组。可以将值包装在std::cmp::Reverse
中,以将默认的最大堆转换为最小堆。
下面是从字符串中提取前5个最频繁字符的代码,使用HashMap进行计数,然后使用BinaryHeap提取顶级值:
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::collections::HashMap;
fn main() {
let text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
let mut map = HashMap::new();
for char in text.chars() {
*map.entry(char).or_insert(0) += 1;
}
let mut heap = BinaryHeap::new();
for (char, count) in map {
heap.push(Reverse((count, char)));
if heap.len() > 5 {
heap.pop(); // Remove the least frequent.
}
}
dbg!(heap
.into_sorted_vec()
.into_iter()
.map(|Reverse(data)| data)
.collect::<Vec<_>>());
}
输出:
[src/main.rs:22] heap.into_sorted_vec().into_iter().map(|Reverse(data)|
data).collect::<Vec<_>>() = [
(
90,
' ',
),
(
59,
'e',
),
(
43,
't',
),
(
39,
's',
),
(
38,
'n',
),
]
https://stackoverflow.com/questions/73012310
复制相似问题