锁定老帖子 主题:面试题目
精华帖 (0) :: 良好帖 (0) :: 新手帖 (16) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-07-30
最烦这个了 有时去大公司人家不管你几年经验了 有时都有这样的题 有什么必要吗?
|
|
返回顶楼 | |
发表时间:2009-07-30
public static void main(String[] args) {
List ls=new ArrayList(); String s= "wklegja;sdfijwekhiopw2903l;k/adddddddddddddddfeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeedbdfsaaaaaaaskldfjpoewgna/'eaegjafdjskl;"; 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; ls.add(tempString); } s = subs; } System.out.println(ls.get(ls.size()-1)+" max= "+max); } |
|
返回顶楼 | |
发表时间:2009-07-30
String str = "hello wolrd wlllkdsfhksadfls?sdfls............................................................... sdf.pqyutgvAAAxzsdfs lsdfj,ljsfd ajfdsak sfksjdfisfsdkfj lsdfjsidf jsafdalsjfs sfskdfjs"; int[] its = new int[200] ; char [] chars = str.toCharArray() ; int max = 0 ; char maxChar = 0 ; for (int i = 0; i < chars.length; i++) { if((chars[i] >= 65 && chars[i] <= 90) || (chars[i] >= 97 && chars[i] <= 122)) { its[chars[i]]++ ; if(its[chars[i]]>=max){ max = its[chars[i]] ; maxChar = chars[i] ; } } } System.out.println("出现最的字符是:"+maxChar+" 出现了"+max+"次"); |
|
返回顶楼 | |
发表时间:2009-07-30
public static void main(String[] args){ String s = "wklegja;sdfijwekhiopw2903l;k/adddddddddddddddfeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeedbdfsaaaaaaaskldfjpoewgna/'eaegjafdjskl;"; s = s.replaceAll("[^a-zA-Z]",""); String ss = null; String _str = null; int _i = 0; for(int i = s.length();i>=1;i=s.length()){ ss = s.substring(0,1); s = s.replaceAll(ss, ""); if(_i<i-s.length()){ _i = i-s.length(); _str = ss; } } System.out.println(_str+":::"+_i); } |
|
返回顶楼 | |
发表时间:2009-07-30
应该是要求不能用JDK的API。
自己写一个实现类似功能的方法。 |
|
返回顶楼 | |
发表时间:2009-07-30
lczheng 写道 楼上实现的过于复杂了,贴一段我的实现,请各位拍砖:
public static void main(String[] args) { Map<String,Long> charTimesMap = new HashMap<String,Long>(); String str = "hello wolrd wlllkdsfhksadfls?sdfls sdf.pqyutgvAAAxzsdfs lsdfj,ljsfd ajfdsak sfksjdfisfsdkfj lsdfjsidf jsafdalsjfs sfskdfjs"; for (char each : str.toCharArray()) { if ((each >= 65 && each <= 90) || (each >= 97 && each <= 122)) { String charStr = String.valueOf(each); if (charTimesMap.containsKey(charStr)) { Long num = charTimesMap.get(charStr).longValue() + 1; charTimesMap.put(charStr, num); } else { charTimesMap.put(charStr, 1L); } } } String maxAppearChar = null; Long maxAppearTimes = 0L; for (Map.Entry<String, Long> charAppear : charTimesMap.entrySet()) { if (charAppear.getValue() > maxAppearTimes) { maxAppearChar = charAppear.getKey(); maxAppearTimes = charAppear.getValue(); } } System.out.println("出现最多的字母:"+maxAppearChar); System.out.println("出现次数:"+maxAppearTimes); } 这个代码写的不错,效率高而且安全,我比较喜欢这个。 |
|
返回顶楼 | |
发表时间:2009-07-30
最后修改:2009-07-31
原来是字母。
|
|
返回顶楼 | |
发表时间:2009-07-30
YiSingQ 写道 lczheng 写道 楼上实现的过于复杂了,贴一段我的实现,请各位拍砖:
public static void main(String[] args) { Map<String,Long> charTimesMap = new HashMap<String,Long>(); String str = "hello wolrd wlllkdsfhksadfls?sdfls sdf.pqyutgvAAAxzsdfs lsdfj,ljsfd ajfdsak sfksjdfisfsdkfj lsdfjsidf jsafdalsjfs sfskdfjs"; for (char each : str.toCharArray()) { if ((each >= 65 && each <= 90) || (each >= 97 && each <= 122)) { String charStr = String.valueOf(each); if (charTimesMap.containsKey(charStr)) { Long num = charTimesMap.get(charStr).longValue() + 1; charTimesMap.put(charStr, num); } else { charTimesMap.put(charStr, 1L); } } } String maxAppearChar = null; Long maxAppearTimes = 0L; for (Map.Entry<String, Long> charAppear : charTimesMap.entrySet()) { if (charAppear.getValue() > maxAppearTimes) { maxAppearChar = charAppear.getKey(); maxAppearTimes = charAppear.getValue(); } } System.out.println("出现最多的字母:"+maxAppearChar); System.out.println("出现次数:"+maxAppearTimes); } 这个代码写的不错,效率高而且安全,我比较喜欢这个。 同意,我思想固化了,留字检讨 |
|
返回顶楼 | |
发表时间:2009-07-30
我觉得那个字符串应该用从键盘上输入的比较好(BuffereReader……)
|
|
返回顶楼 | |
发表时间:2009-07-31
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); } >_< 设个很简练,也很实用 但是我刚看题目想了下,第一个想法是去掉标点符号,然后用subString(0, 1)第一个,然后map.map.containsKey(key),有value+1,没有就map.put(key),如此循环,并且保留value中最大的,以便输出。 没上面的好,但是也是想法! |
|
返回顶楼 | |