Problem Description
Given an array of integers nums
, please sort the array in ascending order of the frequency of each value. If there are multiple values with the same frequency, please sort them in descending order according to the value itself.
Please return the sorted array.
Example 1:
输入:nums = [1,1,2,2,2,3]
输出:[3,1,1,2,2,2]
解释:'3' 频率为 1,'1' 频率为 2,'2' 频率为 3 。
Example 2:
输入:nums = [2,3,1,3,2]
输出:[1,3,3,2,2]
解释:'2' 和 '3' 频率都为 2 ,所以它们之间按照数值本身降序排序。
Example 3:
输入:nums = [-1,1,-6,4,5,-6,1,4,1]
输出:[5,-1,4,4,-6,-6,1,1,1]
Leetcode original title address: https://leetcode.cn/problems/sort-array-by-increasing-frequency
Map collection and two-dimensional array of knowledge point review
We know that the Map collection can be used to store a set of key-value pair data. For example, we want to create a Map collection, there are three sets of data in the collection, corresponding to name and age.
The original data is as follows:
姓名 年龄
孙悟空 500
猪八戒 88
沙和尚 1000
The created Map collection is as shown below
Next, review the two ways to create this Map collection
Use the Map.set() method to create a Map collection
In general, we use the set method of Map to create the set we want, as follows:
let map1 = new Map()
map1.set('孙悟空', 500)
map1.set('猪八戒', 88)
map1.set('沙和尚', 1000)
console.log('map1', map1);
Create a Map collection using a 2D array
既然Map集合可以存储一组组键值对的数据,那么我们可以把这个一组组数据,当成一个个只包含两个项(第0项是键key、第二项是值value)的数组,然后把整个集合当成一个大数组。
So a two-dimensional array came into being. Therefore, with the cooperation of the constructor, we can pass in a two-dimensional array when instantiating the Map object, which can achieve the same effect, as follows:
let map2 = new Map(
[
['孙悟空', 500],
['猪八戒', 88],
['沙和尚', 1000],
]
)
console.log('map2', map2);
From the above two cases, we can know that the Map collection seems to be a deformation of the two-dimensional array, a special two-dimensional array, of course, it is not actually.
- The above case using a two-dimensional array to create a Map collection can be understood as converting a two-dimensional array into a corresponding Map collection;
- Likewise,
Map集合也可转为二维数组,使用Array.from(map)即可
Note that the above two-dimensional array to Map collection case is a fixed number of items. For example, the inner array has only two key items and value items. If there are more than a few items, subsequent items will be ignored. Because a set of data in the Map collection has only key and value. The following code
let map = new Map(
[
['孙悟空', 500, '花果山水帘洞'],
['猪八戒', 88, '高老庄'],
['沙和尚', 1000, '通天河'],
]
)
console.log('map', map);
The result printed is as follows:
{'孙悟空' => 500, '猪八戒' => 88, '沙和尚' => 1000}
The third item of the inner array in the two-dimensional array, 'Huaguoshan Shuiliandong, Gaolaozhuang, Tongtianhe', will be ignored, because each item in the Map collection can only store two items, the key-value pair
Why mention two-dimensional arrays? because the subject requires
problem-solving analysis
-
第一步,使用Map集合统计数组中每一项出现的次数(频率)
-
第二步,因为要把数组做升序排序,所以需要将Map集合转化成二维数组,并调用Array.sort()方法给二维数组做排序
-
然后再创建一个空数组,双层遍历二维数组,将对应项,追加到空数组中去,并返回即可
problem solving code
Let's understand the use case of the nums array defined below
let nums = [1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 4, 3]
var frequencySort = function (nums) {
// 第一步,使用Map集合统计各项出现的次数
let map = new Map()
for (let i = 0; i < nums.length; i++) {
if (map.has(nums[i])) {
let count = map.get(nums[i])
count = count + 1
map.set(nums[i], count)
} else {
map.set(nums[i], 1)
}
}
// 第二步,将统计好频率次数的Map集合转成二维数组并相应排序
let mmap = Array.from(map) // 注意这里要排序两次
mmap.sort((a, b) => {
return b[0] - a[0] // 第一次排序,按照自身从大到小
})
mmap.sort((a, b) => {
return a[1] - b[1] // 第二次排序,按照出现的频率从小到大
})
// 第三步,定义空数组,并循环二维数组追加即可
let result = []
mmap.forEach((item) => {
for (let j = 0; j < item[1]; j++) { // 注意j小于item[1],item[1]即为出现的次数
result.push(item[0]) // item[0] 指的是原始数组中的每一项;item[0]、item[1] 即为 谁、出现了几次
}
})
return result
};
console.log('结果', frequencySort(nums)); // [4, 3, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1]
Summarize
The main test questions are:
- Use of Map Collection
- Simple application of two-dimensional array
- Sorting a 2D Array
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。