★查詢給定字符串中,出現最多的字符和出現的次數


/** * ★查詢給定字符串中,出現最多的字符和出現的次數 * 我在筆試的時候也幾乎這麼寫的。可是咱們技術總監說,這個效率是比較差的。 * 若是20個字符,有一個字符出現了11次,那麼這個字符就是最多的。 */ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CharCount { /*public static void Charcount(String string) { if (string == null) return; int[] count = new int[string.length()]; for (int i = 0; i < count.length; i++) { // 將字符串中索引字符存在臨時變量中 char mid = string.charAt(i); for (int j = 0; j < count.length; j++) { if (mid == string.charAt(j)) count[i]++; } } // 獲得次數最多的字符 int index = 0; int max = count[0]; for (int i = 0; i < count.length; i++) { if (max < count[i]) { max = count[i]; index = i; } } System.out.println(); System.out.println("字符=" + string.charAt(index)); System.out.println("次數=" + count[index]); } */ public static void main(String[] args) { //Charcount("ABAACGDHGSTHJHH"); //findChar("ABAACGDHGSTHJHH"); findMaxCountChar("ABAACGDHGSTHJHH"); } /*public static void Charcount(String string) { //ArrayList 保存字符串中出現的字符 ArrayList list=new ArrayList(string.length()); for(int i=0;i<string.length();i++){ //只要表中不包含,將它添加到表中 if(!list.contains(string.charAt(i))){ list.add(string.charAt(i)); } } int[] count = new int[list.size()]; //這裏外循環就不用遍歷整個字符串,僅僅按照List表中所保存的字符進行遍歷 for(int i=0;i<list.size();i++){ char mid=(Character) list.get(i); for(int j=0;j<string.length();j++){ if(mid==string.charAt(j)){ count[i]++; } } } int index = 0; int max = count[0]; for (int i = 0; i < count.length; i++) { if (max < count[i]) { max = count[i]; index = i; } } System.out.println("字符串中出現的字符:"); for(int k=0;k<list.size();k++) { System.out.print(list.get(k)+" "); } System.out.println(); System.out.println("字符" + list.get(index)+"出現的次數最多"); System.out.println("共出現次數爲: " + count[index]); }*/ /*public static void findChar(String msg){ if(null == msg) return; Map<String,String> charMap = new HashMap<String,String>(); int base = 0; String maxChar = ""; for(int i=0; i < msg.length(); i++){ String word = String.valueOf(msg.charAt(i)); if(!charMap.containsKey(word)){ charMap.put(word, "1"); }else{ int index = Integer.valueOf(charMap.get(word)) + 1; charMap.put(word, String.valueOf(index)); if(index > base){ base = index; maxChar = word; } } } // 出現最屢次數 System.out.println(base); // 出現作屢次數字符 System.out.println(maxChar); }*/ public static void findMaxCountChar(String str) { if (str == null || str == "") return; Map<String, Integer> map = new HashMap<String, Integer>(); int maxCount = 0; String maxCountStr = ""; List<String> list = new ArrayList<String>(); for (int i = 0; i < str.length(); i++) { String key = String.valueOf(str.charAt(i)); if (maxCount >= str.length() / 2) { if (maxCountStr == key) { maxCount++; } } else { if (!map.containsKey(key)) { map.put(key, 1); } else { int count = map.get(key); map.put(key, count + 1); if ((count + 1) > maxCount) { maxCount = count + 1; maxCountStr = key; list.clear(); list.add(key); } else if ((count + 1) == maxCount) { list.add(key); } } } } System.out.println("最大次數" + maxCount); if (maxCount >= str.length() / 2) { System.out.println("字符" + maxCountStr); } else { System.out.println("字符" + list); } } }

 
轉載:http://www.cnblogs.com/java-class/archive/2013/04/25/3043444.html#2666696感謝分享,一塊兒成長!歡迎評論,交流。
相關文章
相關標籤/搜索