题解 | #字符串排序#

字符串排序

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();
            }
        }
    }
}

全部评论

相关推荐

投递腾讯等公司8个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务