题解 | #字符统计#
字符统计
http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
TreeMap存放,自动按ASCII码排序
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
//将字符存入TreeMap
TreeMap<Character,Integer> map = new TreeMap<>();
for (int i = 0; i < str.length(); i++) {
if(!map.containsKey(str.charAt(i))){
map.put(str.charAt(i),1);
}else{
map.put(str.charAt(i),map.get(str.charAt(i))+1);
}
}
//找到max值
int max = 0;
for (int val:map.values()) {
if(val>max)
max = val;
}
//输出
while (max>0){
for (char key:map.keySet()){
if(map.get(key)==max)
System.out.print(key);
}
max--;
}
}
}