一些关于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. uniapp-时间组件

    可以选择年月日时分秒 示例文件 my-datetime.zip 文档:https://ext.dcloud.net.cn/plugin?id=5603

  2. sql 行转列分析 以后再也不用记了

    表scores 请转成的横表是这样子的: // ::::我们来分析下:首先 姓名这一列肯定是不重复的姓名[重复也没办法了 这需求只能这样了] 因此 我们用group by [姓名] 然后姓名这一列就有 ...

  3. mac os 11 Big Sur 根目录无法写入解决办法

    本文目的是解决无法在 / 目录下创建目录的问题: 关闭SIP 重启机器,按住 command + R 选择 磁盘工具 在导航栏 窗口 中打开 终端 ,执行如下命令: csrutil status ## ...

  4. java8 升级 17 兼容测试 emt4j

    测试兼容性的,emt4j 在readme 里 download 节目,点击下载 https://github.com/adoptium/emt4j /root/emt4j-0.3/bin/analys ...

  5. nohup文件的压缩分割

    编写sh脚本 先拷贝,之后,清空. 待完成,压缩功能 #!/bin/sh #description split logs time1=$(date -d 'yesterday' "+%Y%m ...

  6. springsecurity maven 打包后,404错误。maven 打包后,加载内置的xml文件

    404错误,解决的办法,主要是pom文件 <build> <resources> <resource> <directory>src/main/reso ...

  7. centos 防火墙开放端口

    centos防火墙开放端口 开启防火墙 systemctl start firewalld 开放指定端口 firewall-cmd --zone=public --add-port=<PORT& ...

  8. tapdata问题

    聚合节点写两个不同的聚合函数,只需要在关联目标节点的目标字段中添加上分组字段,其他字段不用做关联 聚合节点写两个相同的聚合函数,只需要在关联目标节点的目标字段中左右两边都添加上_id,会输出两条数据, ...

  9. Java实现输出三角形

    本来是想实现空心的,想了太久没写出来就算了 代码 package com.qaq.strruct;public class TestDemo01 { //打印三角形 //思路:空格的距离和星星的数量成 ...

  10. css动画-滚动通知

    使用动画 <div class="horNotice"> <div class="horMove"> <span>累计87例 ...