题解 | #字符串排序#

字符串排序

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

import java.util.*;
import java.util.TreeMap;

//主要复习map的遍历方式。
//排序想到用treemap

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String inputStr = in.nextLine();

        TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
        while (in.hasNext()) {
            String key = in.nextLine();
            if (tm.containsKey(key)) {
                tm.put(key, tm.get(key) + 1);
            } else {
                tm.put(key, 1);
            }
        }

        //遍历map, entry组成的set集合,entryset
        for (Map.Entry<String, Integer> entry : tm.entrySet()) {
            //可能会出现重复单词的情况,有几个就输出几个
            for (int i = 0; i < entry.getValue(); i++) {
                System.out.println(entry.getKey());
            }
        }

    }
}

全部评论

相关推荐

04-01 12:25
中南大学 Java
枯基Evan_:腾讯一面写过11次的题目没写出来
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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