题解 | #字符统计#

字符统计

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

import java.util.*;
import java.util.stream.Stream;

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

        ArrayList<String> arrayList = new ArrayList<>();
        for (String s : str.split("")) {
            arrayList.add(s);
        }

        HashMap<String, Integer> HM = new HashMap<>();
        for (String s : arrayList) {
            if (HM.get(s) == null) {
                HM.put(s, 1);
            } else {
                HM.put(s, HM.get(s) + 1);
            }
        }
        
        HM.keySet().stream().sorted((String o1, String o2)-> {
            if (HM.get(o1) - HM.get(o2) > 0)   return -1;
            else if (HM.get(o1) - HM.get(o2) < 0) return 1;
            else return o1.compareTo(o2);
        })
        .forEach(s -> System.out.print(s));
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 18:00
点赞 评论 收藏
分享
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 10:56
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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