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 ...
随机推荐
- kubectl --v日志级别
Kubectl 日志输出详细程度是通过 -v 或者 --v 来控制的,参数后跟了一个数字表示日志的级别.Kubernetes 通用的日志习惯和相关的日志级别在 这里 有相应的描述. 详细程度 描述-- ...
- jenkins 连接Windows
1.Windows机器需要安装powershell Server 下载路径:https://www.nsoftware.com/powershell/server/ 点击startk开启该服务 2.配 ...
- 基于ipset的dns代理
###基于源IP的dns白名单 sleep 60s/etc/init.d/firewall restart ###创建ipset集合 命名为src_dns_whitelist#清空原有命名为src_d ...
- SAP管理员SAP*和DDIC被锁定后如何解锁或重置密码
SAP*初始化密码是06071992或passDDIC默认密码为19920706 环境信息:win server2003,SQL Server2008 R2 账号信息存在于数据库usr02表中,1.删 ...
- APP对接支付宝付款
1.支付宝开发平台:https://openhome.alipay.com/ 2.进去沙箱,配置支付密钥 记录下appid,然后设置秘钥; 3.后端代码,配置类 pom文件sdk添加依赖 <! ...
- 获取网页数据 Qt 从客户端发起http响应
Qt 从客户端发起http响应 获取网页数据实现代码 void Test::GetHttp(QString strServerIP,QString strUserName,QString strPas ...
- docker打包java
java1.8镜像 localtime文件为/etc/localtime 将本地此文件打包到docker镜像中 FROM openjdk:8-jdk-alpine MAINTAINER geyanan ...
- 12个有用的JavaScript数组技巧
数组是Javascript最常见的概念之一,它为我们提供了处理数据的许多可能性,熟悉数组的一些常用操作是很有必要的. 1.数组去重 1.from()叠加new Set()方法 字符串或数值型数组的去重 ...
- 4. 模板解析,生成render函数,渲染页面
解析模板,生成render函数,执行render函数,实现视图渲染 1.模板转化成ast语法树 2.ast语法树生成render函数 3.执行render函数生成虚拟dom 4.执行_update方法 ...
- R7800 2.4G webshell
webshell执行 iwpriv ath1 disablecoext 1