记录一下TreeMap对数据的排序,有点像桶排
public static void mysort(int[] nums)
{
TreeMap<Integer,Integer>data = new TreeMap<>();
for(int i=0;i<nums.length;i++)
data.put(nums[i],data.getOrDefault(nums[i],0)+1);
int j=0;
Iterator it = data.entrySet().iterator();
while(it.hasNext())
{
Map.Entry<Integer,Integer>entry = (Map.Entry)it.next();
int size = entry.getValue();
int key = entry.getKey();
while(size--!=0)
nums[j++] = key;
}
}
本文介绍了一种使用TreeMap进行数组排序的方法,通过将数组元素作为键存入TreeMap,利用其内部排序特性实现数据排序。文章详细展示了排序过程,包括元素的插入、计数以及按顺序重建数组。
1234

被折叠的 条评论
为什么被折叠?



