一些关于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的更多相关文章

  1. LeetCode 82,考察你的基本功,在有序链表中删除重复元素II

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第51篇文章,我们来看LeetCode第82题,删除有序链表中的重复元素II(Remove Duplicates ...

  2. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  3. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  4. [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 ...

  5. 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 ...

  6. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  7. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  8. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  9. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  10. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. TPS/QPS/RT的关系及计算

    1.定义 TPS:Transactions Per Second,意思是每秒事务数,具体事务的定义,都是人为的,可以一个接口.多个接口.一个业务流程等等.一个事务是指事务内第一个请求发送到接收到最后一 ...

  2. Java基础__01.环境安装

    该篇文章,主要讲述了Java的一些基础知识及准备工作. Java的特性 简单性 面向对象 可移植性,即跨平台性(Write Once, Run Anywhere) 高性能 分布式 动态性 多线程 安全 ...

  3. 学习-Vue2-Vue实例-数据与方法-Object.freeze()

    Object.freeze(),会阻止修改现有的property,意味着响应系统无法再追踪变化 代码示例: <!DOCTYPE html> <html lang="en&q ...

  4. Oracle重建索引

    创建表 create table student( student_id number, name varchar2(240) ) tablespace school_data; 创建索引 creat ...

  5. idea插件Tranlation配置有道搜索引擎

    idea配置有道翻译引擎 一.更换翻译引擎原因 由于Google在2022年9月末宣布关闭GoogleTranslate在中国的服务,原本在chrome浏览器和idea上使用的google翻译引擎也不 ...

  6. CAD坐标显示不全怎么办?CAD坐标常见问题解答!

    今天小编来和大家聊一下浩辰CAD看图王中关于CAD坐标的那些事,比如:CAD坐标为何显示不全?CAD坐标显示结果和之前不一样?以及不能精准捕捉CAD坐标等情况,应该如何轻松解决?今天就和小编一起来了解 ...

  7. Perl 简单脚本处理log信息

    执行了一段命令之后爆出了很多错误怎么收集 比如我们在编译一个大型项目时,编译出了很多报错,我们想收集出编译出错的每一行,肉眼看效率很低,在windows下没有grep怎么办呢? 在学习语法方面,使用p ...

  8. android本地文件处理的一些经验

    选择文件后,现在一般返回 Uri contentResolver.getType(selUrl)结果如下 .txt text/plain .jpeg image/jpeg .mp4 video/mp4 ...

  9. (二)REDIS-重要概念与原理

    1 Redis的概念: Redis是一种key-value类型的内存数据库,可以用于保存string,list,set,sorted set,hash等多种数据结构.由于整个数据库统统加载在内存中进行 ...

  10. axios进行图片上传

    进行图片的上传,一定要注意headers的设置:             headers:{"Content-Type":"multipart/form-data&quo ...