题解 | #统计一句话中重复单词的个数#

统计一句话中重复单词的个数

http://www.nowcoder.com/practice/2128e598b5a5407195c31175b5b33360


public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();

        //write your code here......
        char [] lines = line.toCharArray();
           for(int i=0;i<lines.length;i++){
               if(!(lines[i] >= 'a' && lines[i] <= 'z' ||lines[i] >= 'A' && lines[i] <= 'Z')){
                    continue;
               }
               
               Integer value = map.get(lines[i]);
               int count = 1;
               if(value != null){
                   count = value + 1;
               }
               map.put(lines[i],count);
           }
        Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
        for (Map.Entry<Character, Integer> entry : entrys) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-29 17:30
找实习找着找着就要进入7月了,马上秋招也要开始了,找实习还有意义吗?
绝迹的星:有面就面, 没面上就当日薪4位数大佬免费培训, 面上了再考虑要不要实习
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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