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 ...
随机推荐
- 计算机科学导论-第三版-学习笔记-chapter1-绪论
小测验的网站找不到,小程序也找不到,这两个部分的习题就不做了. Cengage Learning - Foundations of Computer Science这上面有部分答案.这里用斜体标出.我 ...
- vscode 右键运行php文件到浏览器
1.安装PHP Server插件 2.在需要打开的文件中右键选择PHP Server:Server project 3.浏览器页面显示
- grep 查找字符串 在文件或者文件夹中
1, 命令行能做的事情很多, grep 'XXX' ./access.log 当前某个文件下下查找某个字符串grep 'xxx' ./ -r 当前目录文件夹下查找某个字符串
- mmdetection3d安装
conda create -n openmmlab python=3.7 -y conda activate openmmlab pip install torch==1.8.1+cu101 torc ...
- mybatisplus SQL一对多
https://blog.csdn.net/Isyoubao/article/details/122212113 重点:<collection property="nspSchedul ...
- NC16644【字符串的展开】
正确代码: #include <iostream>#include <algorithm>using namespace std;bool IsSame(char a, cha ...
- 基于recorder.js H5录音功能
兼容性 1.Chrome,FF,Edge,QQ,360(注:现有IE和Safari全版本不兼容) 2.其中Chrome47以上以及QQ浏览器强制要求HTTPS的支持 3.请尝试使用FF,Edge,36 ...
- 查看Sql数据库连接数
select count(1) as nums, sd.name from [Master].[dbo].[SYSPROCESSES] spinner join [Master].[dbo].[SY ...
- Flutter 中的普通路由、普通路由传值、 命名路由、命名路由传值
一.Flutter 中的路由 Flutter 中的路由通俗的讲就是页面跳转.在 Flutter 中通过 Navigator 组件管理路由导航.并提供了管理堆栈的方法.如:Navigator.push ...
- mac怎么设置开机自启动项,mac选择开机启动项
转自 https://www.zhangshilong.cn/work/386853.html Login Items Mac OSX的当前用户成功登录后启动的程序,该类别的启动项配置文件存放在~/L ...