public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> words = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
words.add(reader.readLine());
}
Map<String, Integer> map = countWords(words);
for (Map.Entry<String, Integer> pair : map.entrySet()) {
System.out.println(pair.getKey() + " " + pair.getValue());
}
}
public static Map<String, Integer> countWords(ArrayList<String> list) {
HashMap<String, Integer> result = new HashMap<String, Integer>();
//在此编写你的代码
for (int i = 0; i < list.size(); i++) {
if(result.containsKey(list.get(i))){
int count=result.get(list.get(i));
count++;
result.put(list.get(i),count);
}else {
result.put(list.get(i),1);
}
}
return result;
}
}
逻辑有点不容易啊
正在讨论
评论
- 受欢迎
- 新
- 旧
你必须先登录才能发表评论
此页面还没有任何评论