Open yankewei opened 1 year ago
class Solution {
/**
* @param Integer[] $nums
* @return Integer[]
*/
function frequencySort($nums) {
$count_by_num = array_count_values($nums);
usort($nums, function ($one, $two) use ($count_by_num) {
if ($count_by_num[$one] === $count_by_num[$two]) {
return $two <=> $one;
}
return $count_by_num[$one] <=> $count_by_num[$two];
});
return $nums;
}
}
给你一个整数数组
nums
,请你将数组按照每个值的频率升序排序。如果有多个值的频率相同,请你按照数值本身将它们降序排序。请你返回排序后的数组。
示例 1:
示例 2:
示例 3:
提示:
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/sort-array-by-increasing-frequency 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。