题解 | #字符统计#
字符统计
https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String input = in.nextLine(); Map<Character, Integer> map = new TreeMap<>(); for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (map.containsKey(c)) { map.put(c, map.get(c) + 1); } else { map.put(c, 0); } } Integer[][] intArray = new Integer[map.size()][2]; char[] chars = new char[map.size()]; int index = 0; for (Map.Entry<Character, Integer> entry : map.entrySet()) { chars[index] = entry.getKey(); intArray[index][0] = entry.getValue(); intArray[index][1] = index; index++; } Arrays.sort(intArray, (o1, o2) -> o2[0] - o1[0]); for (int i = 0; i < intArray.length; i++) { System.out.print(chars[intArray[i][1]]); } } } }