/**
* 查找字符串中出现次数最多的字符
* @param str 待查找的字符串
* @return 字符
* @throws Exception 输入的字符串为空或者没有内容
*/
private static char find(String str) throws Exception {
if (str != null && str.length() > 0) {
int[] map = new int[256];
Arrays.fill(map, 0);
int length = str.length();
for (int i = 0; i < length; i++) {
map[str.charAt(i)] += 1;
}
int times = 0;
char result = 0;
for (int i = 0; i < map.length; i++) {
if (map[i] > times) {
times = map[i];
result = (char) i;
}
}
return result;
}else {
throw new Exception("str is null or empty");
}
}
查找字符串中出现次数最多的字符
最新推荐文章于 2023-11-21 08:00:00 发布