题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        try{
            
            Map<String,Integer> map = new TreeMap<>();
            int n = in.nextInt();
            while(n>0){
                String str = in.next();
                if(map.containsKey(str)){
                    map.put(str,(Integer)map.get(str) + 1);
                }else{
                    map.put(str,1);
                }
                n--;
            }
            for(String i : map.keySet()){
                if(map.get(i) > 1){
                    for(int j = 1; j <= map.get(i) ;j++){
                        System.out.println(i);
                    }
                }else{
                    System.out.println(i);
                }
            }

        }catch(InputMismatchException e){
            System.out.print("你的输入有误");
        }finally{
            if(in != null){
                in.close();
            }
        }
    }
}

全部评论

相关推荐

求个付费实习岗位:这种就是吃满时代红利又没啥技术水平,只能靠压力学生彰显优越感的老登,别太在意了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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