当前位置:网站首页>每日一题day5-1636. 按照频率将数组升序排序
每日一题day5-1636. 按照频率将数组升序排序
2022-06-22 10:09:00 【程序猿不脱发2】
题目:
给你一个整数数组 nums ,请你将数组按照每个值的频率 升序 排序。如果有多个值的频率相同,请你按照数值本身将它们 降序 排序。
请你返回排序后的数组。
示例 1:
输入:nums = [1,1,2,2,2,3]
输出:[3,1,1,2,2,2]
解释:‘3’ 频率为 1,‘1’ 频率为 2,‘2’ 频率为 3 。
示例 2:
输入:nums = [2,3,1,3,2]
输出:[1,3,3,2,2]
解释:‘2’ 和 ‘3’ 频率都为 2 ,所以它们之间按照数值本身降序排序。
示例 3:
输入:nums = [-1,1,-6,4,5,-6,1,4,1]
输出:[5,-1,4,4,-6,-6,1,1,1]
提示:
1 <= nums.length <= 100
-100 <= nums[i] <= 100
思路:
先定义一个类,记录每个元素的元素值和频率,使用列表存储全部二元组,并对列表排序。
如果两个二元组的频率不同,则根据频率升序排序;
如果两个二元组的频率相同,则根据元素值降序排序。
遍历排序后的列表,对于列表中的每个二元组,将元素值根据频率填入排序后的数组中。遍历结束之后即可得到完整的排序后的数组。
java代码:
class Solution {
class Pair {
int num;
int freq;
public Pair(int num, int freq) {
this.num = num;
this.freq = freq;
}
}
public int[] frequencySort(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int n : nums) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
List<Pair> pairs = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
pairs.add(new Pair(entry.getKey(), entry.getValue()));
}
Collections.sort(pairs, (a, b) -> {
if (a.freq != b.freq) {
return a.freq - b.freq;
} else {
return b.num - a.num;
}
});
int[] sorted = new int[nums.length];
int index = 0;
for(Pair pair:pairs) {
int num = pair.num;
for(int i=0;i<pair.freq;i++) {
sorted[index++] = num;
}
}
return sorted;
}
}
边栏推荐
- Bloom filter optimization - crimsondb series of papers (I)
- C语言编写一个双向链表
- [popular science] to understand supervised learning, unsupervised learning and reinforcement learning
- 从MVC原理开始手敲一个MVC框架,带你体会当大神的乐趣
- Tiktok practice ~ one click registration and login process of mobile phone number (verification code)
- PowerDesigner技巧2 触发器模板
- 呼叫中心专业术语
- Summary of neural network training trick
- Niuke.com Huawei question bank (31~40)
- The solution for golang Mongo go driver find() to read 101 records by default
猜你喜欢

QT compile the Internet of things management platform 36- communication protocol

TikTok 宣布将数据存储于 Oracle 服务器!

不要再傻傻分不清 hash、 chunkhash 和 contenthash 啦

Quickly master asp Net authentication framework identity - login and logout

使用pytorch mask-rcnn进行目标检测/分割训练

Cache penetration tool "Bloom filter"

Record a time when Kali encounters vmtools button gray install vmtools
![[popular science] to understand supervised learning, unsupervised learning and reinforcement learning](/img/24/d26c656135219a38fd64e4d370c9ee.png)
[popular science] to understand supervised learning, unsupervised learning and reinforcement learning

Qt编写物联网管理平台36-通信协议

【深度学习】不得了!新型Attention让模型提速2-4倍!
随机推荐
6-35 constructing an ordered linked list
px4代码学习之基于uavcan协议的时间同步消息中添加自定义字段
C语言编写一个双向链表
Ctfshow Web Learning Records
使用 Matplotlib 这么久,竟不知道数据可以动起来
大学生期末考试SQL语句
使用的软件是php mysql 数据库
PAT甲级 - 1013 Battle Over Cities(删点判连通块数量)
6-40 constructing ordered sparse polynomial chained storage
PAT甲级 - 1012 The Best Rank(坑点)
钟珊珊:被爆锤后的工程师会起飞|OneFlow U
坚决打好打赢防汛救灾这场硬仗 全力保障人民群众生命财产安全
三个月让软件项目成功“翻身”!
[structure training camp - module 3]
Learning serialization and deserialization from unserialize3
C language to write a two-way linked list
Cobalt strike from entry to imprisonment (III)
Pareto's law at work: focus on results, not outputs
Qt编写物联网管理平台36-通信协议
IPO Configuration Guide