一些关于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. oracle ebs 加锁

    DECLARE -- lock l_lockname VARCHAR2(100); l_lockhandle VARCHAR2(200); l_lock_output NUMBER; l_locked ...

  2. 戴尔n4110的独显驱动黄色感叹号问题的解决方法

    直接开门见山: 要下载旧版驱动R302028.exe,但是戴尔似乎下掉了,找到了代替的地址 下载地址如下: http://soft.onlinedown.net/soft/556280.htm ,下载 ...

  3. python缩小放大浏览器

    driver.execute_script("document.body.style.zoom='70%'") driver.execute_script("docume ...

  4. Review1(C#语言基础)

    MeshFilter决定了物体时什么形状 MeshRender决定了物体时的外观: 运行时常量:readonly 1.readonly string NAME_READONLY = "rea ...

  5. java.3 Java数据类型1

    数据类型讲解 Java属于强类型语言 即要求变量的使用要严格符合规定,所有变量都必须严格定义以后才能使用 优点:安全性高 缺点:速度慢(相对而言) Java数据类型分为引用类型和基本类型 基本数据类型 ...

  6. android cannot generate view binders android.databinding.tool.util.LoggedErrorException

    错误: Cannot resolve type 'viewModel'错误: cannot generate view binders android.databinding.tool.util.Lo ...

  7. BottomNavigationBar 自定义 底部导航条、以及实现页面切换

    一.Flutter BottomNavigationBar 组件 BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Sc ...

  8. EasyExcel工具使用

    pom文件: <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel< ...

  9. Python学习笔记调式之抛出异常

    随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...

  10. 数据库管理工具naicat+DG

    DG 参考链接:https://www.cnblogs.com/zuge/p/7397255.html 自我感觉: 亲切,万能,idea用多了... 石皮 解 用学生账号登陆就可以(我用的这一种) 工 ...