题解 | #字符统计#

字符统计

http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.next();
            Map<Character, Integer> map = new LinkedHashMap<>();
            List<Character> list = new ArrayList<>();
            for (int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);
                if (map.containsKey(ch)) {
                    map.put(ch, map.get(ch) + 1);
                } else {
                    map.put(ch, 1);
                    list.add(ch);
                }
            }
            Collections.sort(list);
            char[] chars = new char[list.size()];
            for (int i = 0; i < chars.length; i++) {
                chars[i] = list.get(i);
            }
            for (int i = 0; i < chars.length - 1; i++) {
                for (int j = 0; j < chars.length - 1 - i; j++) {
                    if (map.get(chars[j]) < map.get(chars[j + 1])) {
                        char temp = chars[j];
                        chars[j] = chars[j+1];
                        chars[j+1] = temp;
                    }
                }
            }
            for (int i = 0; i < chars.length; i++) {
                if (i == chars.length - 1) {
                    System.out.println(chars[i]);
                } else {
                    System.out.print(chars[i]);
                }
            }
        }
    }
}
全部评论

相关推荐

每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务