相同出现频率下,需要把数字大的排在前面,我们可以把数字变负,这样,排序时大的数字会排在前。在append时变回原来数字即可。
class Solution:
def frequencySort(self, nums: List[int]) -> List[int]:
Count = collections.Counter(nums)
Container = []
for num, fre in Count.items():
num = -1*num
Container.append((fre, num) )
Container.sort(key = lambda x:(x[0],x[1]))
res = []
for fre, num in Container:
num = -1*num
for _ in range(fre):
res.append(num)
return res