最近面试时碰到一道需要上机的题目要我用手写出来。我当时没做出来。回家后就自己写了下。可能不是最好的,但还是我自己做出来的欢迎拍砖
题目很简单:给你一个字符串,包含了空格等标点符号,要你计算出出现次数最多的字母和该字母出现的次数。
下面是我的方法
package demo;
import java.util.*;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class TestClass {
/**
* @param args
*/
//去重复字母Map
private static Map<String, String> chMap = new HashMap<String, String>();
//对每个字母个数进行保存Map,后面的value可以不用List改成其他类型更好(Integer)
private static Map<String, List> countMap = new HashMap<String, List>();
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "hello wolrd wlllkdsfhksadfls?sdfls sdf.pqyutgvAAAxzsdfs lsdfj,ljsfd ajfdsak sfksjdfisfsdkfj lsdfjsidf jsafdalsjfs sfskdfjs";
getChar(str);
int chMpSize = chMap.size();
int countMapSize = countMap.size();
System.out.println("chMpSize: " + chMpSize );
System.out.println("countMapSize: " + countMapSize );
System.out.println("===================开始=======================");
String [] sArray = str.split("");
List l = new ArrayList();
List countArray = new ArrayList();
for(int i=0;i<sArray.length;i++){
//System.out.println(i + " " + sArray[i]);
//int flag = 0;
if(!sArray[i].equals("") && !sArray[i].equals(" ")
&& !l.contains(sArray[i]) && !sArray[i].equals(",")
&&!sArray[i].equals(".") && !sArray[i].equals("!")
&&!sArray[i].equals("?")){
//l.add(sArray[i]);
int num = countMap.get(sArray[i]).size();
countArray.add(num);
}
}
//排序默认是升序
Arrays.sort(countArray.toArray());
Collections.reverse(countArray);
//System.out.println(countArray.get(0));
int max = Integer.parseInt(countArray.get(0)+"");
//System.out.println("tt" + sArray.length);
for(int i=0;i<sArray.length;i++){
if(!sArray[i].equals("") && !sArray[i].equals(" ")&& !l.contains(sArray[i])
&& !sArray[i].equals(",") &&!sArray[i].equals(".") && !sArray[i].equals("!")
&&!sArray[i].equals("?")){
l.add(sArray[i]);
int num = countMap.get(sArray[i]).size();
System.out.println("字母为: " + sArray[i] + " 次数: " + num);
if(countArray != null && countArray.size()!=0 && max == num){
System.out.println("字母为: " + sArray[i] + " 最大次数 " + num);
}
}
}
}
public static void getChar(String str){
//去掉空格
String[] s = str.split(" ");
char [] c = null;
if (s != null){
for(int i=0;i<s.length;i++){
c = s[i].toCharArray();
getChar(c);
}
}
}
public static void getChar(char [] ch){
if(ch != null && ch.length>0){
for(int i=0;i<ch.length;i++){
List list = new ArrayList();
String tmp = ch[i]+"";
if(!tmp.equals(",") && !tmp.equals("!")
&& !tmp.equals("?") && !tmp.equals(".")){
//第一次
if(!chMap.containsKey(ch[i]+"")){
chMap.put(ch[i]+"", ch[i]+"");
list.add(ch[i]);
if(!countMap.containsKey(ch[i]+"")){
countMap.put(""+ch[i], list);
}
}else{
list = countMap.get(""+ch[i]);
list.add(ch[i]);
countMap.put(""+ch[i], list);
}
}
}
}//end if
}
}
---------------------------------------------------------------------------------------------------
楼上实现的过于复杂了,贴一段我的实现,请各位拍砖:
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);
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
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);
}