leetcode 1636
一些关于hashmap和list的用法
class Solution {
public int[] frequencySort(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();
for (int num : nums) {
cnt.put(num, cnt.getOrDefault(num, 0) + 1); 统计频率的常用方法,需要牢记
}
List<Integer> list = new ArrayList<Integer>();
for (int num : nums) {
list.add(num);
}
Collections.sort(list, (a, b) -> {
int cnt1 = cnt.get(a), cnt2 = cnt.get(b);
return cnt1 != cnt2 ? cnt1 - cnt2 : b - a;
}); 重写排序方法中的比较策略(compare方法)
int length = nums.length;
for (int i = 0; i < length; i++) {
nums[i] = list.get(i);
}
return nums;
}
}
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/sort-array-by-increasing-frequency/solution/an-zhao-pin-lu-jiang-shu-zu-sheng-xu-pai-z2db/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
leetcode 1636的更多相关文章
- LeetCode 82,考察你的基本功,在有序链表中删除重复元素II
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第51篇文章,我们来看LeetCode第82题,删除有序链表中的重复元素II(Remove Duplicates ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- 带有关键词的行 txt文本处理
原本的代码(只筛选出来自己要的)([python]文本处理:删除包含关键词的行.删除指定列.删除指定字符.替换关键字--_bandaoyu的博客-CSDN博客_python 删除csv还有特定词语的某 ...
- springboot 集成poi导出word(一)
使用ruoyi-前后端分离版本,根据word模板导出,包含表格和图片. 一.创建模板 列表使用{{}},文本使用[] 二.引入依赖 <!-- excel工具 --> <depende ...
- Tushare金融大数据开放社区 - 数据抽取案例学习
进入平台介绍 扫码立即注册,更多大数据等你来探索 ! 案例: 导入tushare import tushare as ts 这里注意, tushare版本需大于1.2.10 设置token ts.se ...
- Fortran处理无符号整型unsigned integer
背景: 计算机是以一串二进制数,用约定的表示方式来存储数据的.约定表示方式的不同,造成了可以表示数的范围不同.其中,对于整数类型数据的表示,有unsigned integer(无符号整型)和signe ...
- C#中检测代码执行时间
使用System.Diagnostics.Stopwatch, using System; using System.Diagnostics; using System.Threading; clas ...
- https原理(四)双向实践(java客户端+tcp代理)
本文采用客户端与服务端共用一个密钥对 1 将https代理服务器(三)实践中的mkcert p12分解为一个公钥一个私钥 mac@macdeMacBook mkcert % openssl pkcs1 ...
- Dynamics 365 如何代表其他用户发送邮件
举个例子,用户A和用户B,用户B在新建电子邮件时,发件人以用户A的身份去发送邮件,这个时候需要做如下配置才可以. 首先登录用户A,在高级设置->个人设置中,配置允许其他人代表自己发送电子邮件. ...
- javaScript 获取对象数组的对象里面想要的属性,返回一个新的数组
// obj 数组 或者 对象 // arr 要获取对象数组的对象的key数组 // addProperty 可以往对象数组的每一个对象添加一个新的属性 reducedFilter(obj, arr, ...
- 2347. 最好的扑克手牌 (Easy)
问题描述 2347. 最好的扑克手牌 (Easy) 给你一个整数数组 ranks 和一个字符数组 suit .你有 5 张扑克牌,第 i 张牌大小为 ranks[i] ,花色为 suits[i] . ...
- 5. icon创建
1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="U ...