锁定老帖子 主题:面试题目
精华帖 (0) :: 良好帖 (0) :: 新手帖 (16) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-08-14
最后修改:2009-08-14
用map去记录每个字符出现的个数,最后并对map里面value存贮的字母出现的次数,返回排序好的 Map.Entry<String, Integer>[]数组。此代码没有考虑效率。欢迎拍砖
public class WordCount { public static void main(String[] args) { String words = "hello wolrd wlllkdsfhksadfls?sdfls sdf.pqyutgvAAAxzsdfs lsdfj,ljsfd ajfdsak" +" sfksjdfisfsdkfj lsdfjsidf jsafdalsjfs sfskdfjs"; words = words.replaceAll("[^a-zA-Z]", ""); StringCounter strCounter = new StringCounter(); String strTem; for(int i = 0; i < words.length(); i++) { strTem = String.valueOf(words.charAt(i)); strCounter.count(strTem); } //getSortedHashtableByValue(strCounter); //遍历排序好的Map.Entry,从大到小 for(Map.Entry<String, Integer> entry : getSorted(strCounter)) { System.out.println(entry.getKey() + "------------" + entry.getValue()); } } //嵌套类(记录每个字母出现的次数) static class StringCounter extends HashMap<String,Integer> { public void count(String str) { Integer num = get(str); put(str,num == null ? 1 : num + 1); } } //返回排序好的数组 static Map.Entry<String, Integer>[] getSorted(Map<String,Integer> m) { Set set = m.entrySet(); Map.Entry<String, Integer>[] entries = (Map.Entry<String, Integer>[]) set.toArray(new Map.Entry[set.size()]); //Comparator Arrays.sort(entries, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> entry1, Entry<String, Integer> entry2) { Integer in1 = entry1.getValue(); Integer in2 = entry2.getValue(); return in2.compareTo(in1); } } ); return entries; } } |
|
返回顶楼 | |
发表时间:2009-08-15
public static void main(String[] args) throws InterruptedException { String str = "1232aaaaazzzzzccAAAAAAAAAAAAAAacccafsfrerwerzzbb,ccdcc "; int[] words = new int[26]; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c >= 'a' && c <= 'z') { int idx = c - 'a'; words[idx] = words[idx] + 1; } else if (c >= 'A' && c <= 'Z') { int idx = c - 'A'; words[idx] = words[idx] + 1; } } int maxIdx = 0,arrMax = words[0]; for (int i = 1; i < words.length; i++) { if (words[i] > arrMax) { arrMax = words[i]; maxIdx = i; } } System.out.println("出现次数最多的字母是:" + (char)('A' + maxIdx) + " 出现次数是:" + arrMax); } |
|
返回顶楼 | |
发表时间:2009-08-20
看了你的代码,我感觉很糟糕,因為你把问题复杂化了!!!
他并没有说用什么语言来开发,我写了2种方式给你,不能说最好,但是精简 JS版本 <script> var token = {}; var str = "a nn dd sdd sasd2312"; for(var i = 0;i < str.length;i++){ var s = str.charAt(i); if(token[s] >= 1){ token[s]++; } else{ token[s] = 1; } } var result= "" ; for(var p in token){ result += (p+" = "+token[p])+"\n" } alert(result) //TODO 排序获取最大的一个,整个代码不超过30行 </script> JAVA版本 import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; public class Test3 { /** * 计算个数 * @param str * @return */ private Map<Character ,Integer> getToken(String str){ Map<Character ,Integer> token = new LinkedHashMap<Character ,Integer>(); for(int i = 0;i < str.length();i++){ if(token.get(str.charAt(i)) != null){ token.put(str.charAt(i), token.get(str.charAt(i))+1); } else{ token.put(str.charAt(i), 1); } } return token; } public static void main(String[] args) { Test3 t = new Test3(); String str = "a nn aaaaadd sdd sasd2312"; Map<Character ,Integer> token = t.getToken(str) ; Iterator<Character> iterator = token.keySet().iterator(); int max = Collections.max(token.values()); while(iterator.hasNext()){ Character c = iterator.next(); if(token.get(c).compareTo(max) == 0){ System.out.println(c +" = "+token.get(c)); } } } } |
|
返回顶楼 | |
发表时间:2009-08-20
evabibi 写道 public static void main(String[] args) { String s= "sdfsddddddddddfffff,sdfsdf,"; s=s.replaceAll("[^a-zA-Z]",""); int max = 0; int temp = 0; String tempString = ""; for(int i = 0 ;i<s.length() ; i++ ){ tempString = s.substring(0,1); String subs = s.replace(s.substring(0,1),""); temp = s.length() - subs.length(); if(max<temp){ max=temp; } s = subs; } System.out.println(tempString+" max= "+max); } >_< 结果为: f max= 13 正确结果为: d max= 13 漏了个中间变量 否则永远输出的是最后个剩下的字母 public static void main(String[] args) { String s = "sdfsddddddddddfffff,sdfsdf,"; s = s.replaceAll("[^a-zA-Z]", ""); int max = 0; int temp = 0; String tempString = ""; String maxString = ""; for (int i = 0; i < s.length(); i++) { tempString = s.substring(0, 1); String subs = s.replace(s.substring(0, 1), ""); temp = s.length() - subs.length(); if (max < temp) { max = temp; maxString = tempString; } s = subs; } System.out.println(maxString + " max= " + max); } |
|
返回顶楼 | |
发表时间:2009-08-21
用JAVA写,当然要用OOP的思想了,
/******************************************************* 实体类 package Str; public class StrObject { private char name; private int num; /** * @return the name */ public char getName() { return name; } /** * @param name the name to set */ public void setName(char name) { this.name = name; } /** * @return the num */ public int getNum() { return num; } /** * @param num the num to set */ public void setNum(int num) { this.num += num; } } /************************************************************************* /************************************************************************* 主类 package Str; import java.util.*; public class Test { public static void main(String[] args) { String str = "asdfasd asdfwe.asdfasd,erytkcvm,drty sdflkjgsdyvv5458sdgf 454df21f455asdf2r"; Test test = new Test(); test.test(str); } public void test(String str) { List list = new ArrayList(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); StrObject so = new StrObject(); so.setName(c); so.setNum(1); StrObject s = getObject(list, so); if(s == null) list.add(so); else s.setNum(1); } StrObject strObject = null; for (int i = 0; i < list.size()-1; i++) { if(strObject == null) { strObject = (StrObject) list.get(i); continue; } StrObject so = (StrObject) list.get(i); if(so.getNum() > strObject.getNum()) strObject = so; } System.out.println("出现次数最多的:"+strObject.getName()+" 数量:"+strObject.getNum()); } public StrObject getObject(List list,StrObject so) { for (int i = 0; i < list.size(); i++) { StrObject s = (StrObject) list.get(i); if(s.getName() == so.getName()) return s; else continue; } return null; } } /******************************************************************************** |
|
返回顶楼 | |
发表时间:2009-08-22
最后修改:2009-08-22
姐姐,发代码的时候请用[1code=1"1java1"][/1code1]括起来
test方法中的第一个for 循环的最后一句有逻辑错误。我在你的基础上修改了一下,同时也支持多个字符相同的情况。 public void test(String str) { List list = new ArrayList(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); StrObject so = new StrObject(); so.setName(c); so.setNum(1); StrObject s = getObject(list, so); if (s == null) list.add(so); else s.setNum(s.getNum() + 1); } StrObject strObject = null; Vector v = new Vector(); for (int i = 0; i < list.size() - 1; i++) { if (strObject == null) { strObject = (StrObject) list.get(i); v.add(strObject); continue; } StrObject so = (StrObject) list.get(i); if (so.getNum() > strObject.getNum()){ strObject = so; v.clear(); v.add(so); } if (so.getNum() == strObject.getNum()){ v.add(so); } } Iterator it = v.iterator(); while(it.hasNext()){ strObject = (StrObject)it.next(); System.out.println("出现次数最多的:" + strObject.getName() + " 数量:" + strObject.getNum()); } } |
|
返回顶楼 | |
发表时间:2009-08-22
//简单实现了一下楼上的
import java.util.*; public class Count { public static void main(String[] args) { String str = "hello wolrd wlllkdsfhksadfls?sdfls sdf.pqyutgvAAAxzsdfs lsdfj,ljsfd ajfdsak sfksjdfisfsdkfj lsdfjsidf jsafdalsjfs sfskdfjs"; Map<Character,Integer> charMap = new HashMap<Character,Integer>(); for(char each : str.toCharArray()) { if(charMap.containsKey(each)) { charMap.put(each,charMap.get(each) + 1); }else { charMap.put(each,1); } } Set set =charMap.keySet(); ///获得KEY Collection coll = charMap.values(); ///获得VALUE Iterator it = coll.iterator(); Iterator its = set.iterator(); while(it.hasNext()) { System.out.println(its.next()+" :"+it.next()); } } } |
|
返回顶楼 | |
发表时间:2009-08-24
yenan354 写道 姐姐,发代码的时候请用[1code=1"1java1"][/1code1]括起来
test方法中的第一个for 循环的最后一句有逻辑错误。我在你的基础上修改了一下,同时也支持多个字符相同的情况。 (我晕死,你不看下方法就乱说我错误,我的setNum方法里怎么写的你没看到哦,我是写的this.num+=num,看人家的代码也不看仔细,每个人写代码的习惯不同好不好,) public void test(String str) { List list = new ArrayList(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); StrObject so = new StrObject(); so.setName(c); so.setNum(1); StrObject s = getObject(list, so); if (s == null) list.add(so); else s.setNum(s.getNum() + 1); } StrObject strObject = null; Vector v = new Vector(); for (int i = 0; i < list.size() - 1; i++) { if (strObject == null) { strObject = (StrObject) list.get(i); v.add(strObject); continue; } StrObject so = (StrObject) list.get(i); if (so.getNum() > strObject.getNum()){ strObject = so; v.clear(); v.add(so); } if (so.getNum() == strObject.getNum()){ v.add(so); } } Iterator it = v.iterator(); while(it.hasNext()){ strObject = (StrObject)it.next(); System.out.println("出现次数最多的:" + strObject.getName() + " 数量:" + strObject.getNum()); } } |
|
返回顶楼 | |
发表时间:2009-08-24
你们是学JAVA的也,一点OOP的思想都没有,
|
|
返回顶楼 | |
发表时间:2009-08-24
天啦,楼主这样写啊,这是笔试题哦,如果答案是这样,考官都郁闷死了,谁会去看呢?
这样的题目其实非常简单啊~~~先对字符串的内容排序(也可以变成字符数组排序),然后循环一下就可以了啊! 只需要保留一个int变量保存上一个不同字符出现的个数就可以了。(如果考手写,使用commons-lang其实不太可能的) 各位童鞋啊,如果笔试遇到这类问题,而且还是手写的,尽量往简单的想! |
|
返回顶楼 | |