Leetcode 刷题 – 排序(day3)_桶排序_按照字符出现次数对字符串排序
2. 按照字符出现次数对字符串排序
451. Sort Characters By Frequency (Medium)
Input: “tree” Output: “eert” Explanation: ‘e’ appears twice while ‘r’ and ‘t’ both appear once. So ‘e’ must appear before both ‘r’ and ‘t’. Therefore “eetr” is also a valid answer.
实现思路:
1.建立map字典,统计字符串中每个元素出现的次数2.建立集合数组,以map中的值为集合数组下表,map的键为集合数组元素3.将集合数组从大到小赋值给新集合。因为可能r数组中的某些值为null,故要进行该操作
字符串方法简介
/** * 用StringBuilder,StringBuffer,Lis
共有 0 条评论