题解 | #字符统计#

字符统计

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();

        Map<Character, Integer> map = new TreeMap<>();
        for(int i=0; i<str.length(); i++){
            //Set<Character> keys = map.keySet();
            char key = str.charAt(i);
            //if(keys.contains(key)){
            if(map.containsKey(key)){
                map.replace(key, map.get(key)+1);
            }else{
                map.put(key, 1);
            }
        }
        
        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--;
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:24
大家还是用ai改吧,我心疼得要死,就当花钱买教训吧,人家直接拿完钱就跑路了
程序员小白条:简历修改700....神奇,又不是帮你面试,咋的,简历修改从双非变92了还是没实习变成有大厂实习了
点赞 评论 收藏
分享
07-10 14:08
已编辑
江西农业大学 Java
念旧select:做完把项目放到自己硬盘里给他看,看完拷走
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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