一个面试题,当时有思路,却还差点细节,没做出来,大家帮我看看。

qq_29448025 2017-03-22 11:19:28


...全文
770 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zheng.Zeng 2017-04-01
  • 打赏
  • 举报
回复
qq_29448025 2017-03-26
  • 打赏
  • 举报
回复
引用 5 楼 flyskytoday 的回复:
[quote=引用 楼主 qq_29448025 的回复:]
String str="we come on,in the end;this is csdn";//
String[] strList=str.split(" |,|;");
Map<String,int> strMap=new Map<String,int>();
for(String s:strList){
int i=strMap.get(s);
if(i==null){
strMap.put(s,1);
} else {
strMap.put(s,i+1);
}
}
for(String key:strMap.keySet()){
System.out.println(key+"出现了 " +strMap.get(key) +"次");
}
顺便问下,新面试职位的薪水多少啊?[/quote] 试用期5000,转正6000.
逗泥丸的平方 2017-03-24
  • 打赏
  • 举报
回复
我就想问一句 e.V.. 算几个单词
乐之者v 2017-03-24
  • 打赏
  • 举报
回复
引用 7 楼 LinBilin_ 的回复:

/**
 * 统计文本每个单词的个数
 * @param text  文本
 * @param ignoreCase  是否忽略大小写
 * @return
 */
public static	Map<String ,Integer> countEachWorld(String text,boolean ignoreCase){
	Matcher m=Pattern.compile("\\w+").matcher(text);
	String matcheStr=null;
	Map<String ,Integer> map=new LinkedHashMap<>();
	Integer count=0;
	while(m.find()){
		matcheStr=m.group();
		matcheStr=ignoreCase?matcheStr.toLowerCase():matcheStr;
		count=map.get(matcheStr);
		map.put(matcheStr, count!=null?count+1:1);
	}
	return map;
}
这段代码写得好好。这个find和group我不太会用。学习了~ 我可能学了假的正则表达式。哈哈。
qq_34712076 2017-03-24
  • 打赏
  • 举报
回复
引用 8 楼 qq_37826466 的回复:
我的思路是先把里面的逗号和句号给替换成字符串,然后再split(“ ”)按空格分开放到数组里面,然后for循环比较出现相同的次数(选择排序算法思想)
你忽略了/r/n
田地里的蚂蚁 2017-03-23
  • 打赏
  • 举报
回复
乐之者v 2017-03-23
  • 打赏
  • 举报
回复

    //计算字符串中每一个单词出现的个数
    public static Map countWords(String str){
        if(str==null || str.isEmpty() ) {
            System.out.println("字符串为空,无法分隔出单词");
            return null;
        }
       String[] splitResult=str.split("[^A-Za-z]");
       Map<String,Integer> map=new ConcurrentHashMap<String,Integer>();
       map.put(splitResult[0].toLowerCase(),0);
       for(String word:splitResult) {
           Iterator< Map.Entry<String,Integer> > iterator=map.entrySet().iterator();
           String wordLowerCase=word.toLowerCase();
           int n=0;
           while (iterator.hasNext()) {
               Map.Entry<String, Integer> entry = iterator.next();
               if(  wordLowerCase.equals(entry.getKey() ) ){
                   map.put(wordLowerCase,entry.getValue()+1);
                   break;
               }
               n++;
           }
          if(n==map.size() && !wordLowerCase.isEmpty()) {              //如果单词和map里所有的key都不同,就把单词作为新的key加入到map里
               map.put(wordLowerCase,1);
          }
       }
        return  map;
    }



田地里的蚂蚁 2017-03-23
  • 打赏
  • 举报
回复
我的思路是先把里面的逗号和句号给替换成字符串,然后再split(“ ”)按空格分开放到数组里面,然后for循环比较出现相同的次数(选择排序算法思想)
kingofdancer 2017-03-22
  • 打赏
  • 举报
回复
引用 2楼鱿鱼ing 的回复:
这个不用正则,直接split(",") split(".") split(" ") 逗号、句号、空格 这样完了是不是全是单词了?
有道理啊
qq_34750958 2017-03-22
  • 打赏
  • 举报
回复
import java.util.HashMap; import java.util.Map; public class test { public static Map wordcount(String str){ Map map = new HashMap(); String[] word = str.split("\\.|\\s|\\,|\\?"); int count = 0; for(int i=0;i<word.length;i++) { if (!word[i].equals(" ")) { for (int j = 0; j < word.length; j++) { if (word[j].equals(word[i])) { count = count + 1; } } map.put(word[i],count); count=0; } } return map; } public static void main(String[] args) { String str = "May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too."; Map map = test.wordcount(str); System.out.println(map); } } 新手,写的不好,勿喷。
鱿鱼ing 2017-03-22
  • 打赏
  • 举报
回复
这个不用正则,直接split(",") split(".") split(" ") 逗号、句号、空格 这样完了是不是全是单词了?
qq_29448025 2017-03-22
  • 打赏
  • 举报
回复
我的思路如下: 1)用正则表达式来分隔每个单词(当时没想起怎么表示分隔符合适) 2)分隔到字符串数组后,再进行统计。每遇到一个单词,跟前面已参加过统计的单词进行对比,若存在,则直接个数加1;若不存在,则为新单词,个数初始为1 3)最终用一个HashMap<String,count>来存放结果,键是某个具体的单词,count是某个单词的个数。 当时时间有限,加上正则那块有点生疏,虽有思路,还是未能完成。
  • 打赏
  • 举报
回复

/**
 * 统计文本每个单词的个数
 * @param text  文本
 * @param ignoreCase  是否忽略大小写
 * @return
 */
public static	Map<String ,Integer> countEachWorld(String text,boolean ignoreCase){
	Matcher m=Pattern.compile("\\w+").matcher(text);
	String matcheStr=null;
	Map<String ,Integer> map=new LinkedHashMap<>();
	Integer count=0;
	while(m.find()){
		matcheStr=m.group();
		matcheStr=ignoreCase?matcheStr.toLowerCase():matcheStr;
		count=map.get(matcheStr);
		map.put(matcheStr, count!=null?count+1:1);
	}
	return map;
}
zs808 2017-03-22
  • 打赏
  • 举报
回复
单词是要忽略大小写的吧,put到map之前要toLowerCase
flyskytoday 2017-03-22
  • 打赏
  • 举报
回复
引用 楼主 qq_29448025 的回复:
String str="we come on,in the end;this is csdn";//
String[] strList=str.split(" |,|;");
Map<String,int> strMap=new Map<String,int>();
for(String s:strList){
int i=strMap.get(s);
if(i==null){
strMap.put(s,1);
} else {
strMap.put(s,i+1);
}
}
for(String key:strMap.keySet()){
System.out.println(key+"出现了 " +strMap.get(key) +"次");
}
顺便问下,新面试职位的薪水多少啊?

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧