题解 | #字符统计#

字符统计

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]);
                }
            }
        }
    }
}
全部评论

相关推荐

萧索X:写篮球联赛干嘛,陪老板打篮球吗。还有实习经历要写自己所在岗位具体完成什么工作,自己的任务具体完成了什么需求,给公司带来了哪些量化增长
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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