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

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

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

import java.util.*;

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......

/* for(int i = 0;i<line.length();i++){ int count = 0; if((line.charAt(i) >= 'A' && line.charAt(i) <= 'Z') || ((line.charAt(i) >= 'a' && line.charAt(i) <= 'z'))){ for(int j = 0 ; j<line.length();j++){ if(line.charAt(i) == line.charAt(j)){ count++; } map.put(line.charAt(i),count); } }else{ continue; } }*/ for(int i = 0 ; i<line.length();i++){ char ch = line.charAt(i); if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'){ if(map.containsKey(ch) == true){ map.put(ch,map.get(ch)+1); }else{ map.put(ch,1); } } }

    Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
    for (Map.Entry<Character, Integer> entry : entrys) {
        System.out.println(entry.getKey() + ":" + entry.getValue());
    }
}

}

全部评论

相关推荐

收拾收拾准备躺平咯
在秋招的河老师很爱吃:兄弟你吃拼好饭中毒了
点赞 评论 收藏
分享
Sigma429:极兔啊,薪资开的巨低,还在上海,索性不做笔试了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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