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 ...
随机推荐
- 谈谈关于CDN缓存
一.CDN是什么? 谈到CDN的作用,可以用8年买火车票的经历来形象比喻: 8年前,还没有火车票代售点一说,12306.cn更是无从说起.那时候火车票还只能在火车站的售票大厅购买,而我所住的小县城并不 ...
- 解决sqlplus中方向键和退格键乱码问题
centos7 1.安装rlwrap软件包 yum install rlwrap -y 2.编辑环境变量 su - oracle vim ~/.bash_profile alias sqlplus=' ...
- SQL中各种join的区别
INNER JOIN 在表中存在至少一个匹配时,INNER JOIN 关键字返回行 返回的是一个交集 内连接就是等值连接 自然连接(outer join. left join, right join) ...
- Python学习:画K帮
import datetime import pandas_datareader.data as web df_stockload = web.DataReader("600797.SS&q ...
- GNOME 窗口添加最大化、最小化按钮
1.安装工具 使用终端命令安装优化工具 yum install gnome-tweak-tool 2.配置 gnome-tweak-tool 安装完毕后,在应用程序的"工具"中找到 ...
- vue IE9兼容flex布局 css3(转载)
原文 https://blog.csdn.net/shihezhengshz/article/details/118860562 写这文章的时候的我,心力憔悴鸭,找了好长时间,呜呜┭┮﹏┭┮ 好了,开 ...
- VSCode搭建Go语言环境
一.安装go 1. 获取go安装包 https://golang.org/dl/ 2. 本地安装(省略) 3. 配置和环境变量 GO111MODULE 是否支持gomod GOROOT go安装的 ...
- 集成GIT仓库
集成GIT仓库 jgit - java实现git操作 一个 Java 程序中使用 Git ,有一个功能齐全的 Git 库,那就是 JGit . JGit 是一个用 Java 写成的功能相对健全的 Gi ...
- mybatis原理探究
jdbc数据库运行流程: JDBC有哪三种statement接口: Statement 1.Statement接口提供了执行语句和获取结果的基本方法: 2.Statement继承自Wrapper:3. ...
- 区分CommonJs/ES6 Module/AMD/CMD
模块加载方式 CommonJs ES6 Module AMD CMD UMD Commonjs和ES6 Module的区别 总结 1.CommonJS CommonJS 是一个项目,其目标是为 Jav ...