4,022
社区成员
// 第二题 标准的递归
// 第一题很不错,涉及很多 Java 基础知识:字符串、字符、Map、List、循环、迭代器、比较器
import java.util.*;
/**
test
@author hubo
@since 2021/10/27 8:56
/
public class Test {
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.charCountStatistical("aabbbc"));
}
/**
Description:
@author hubo
@date 2021/10/27 8:58
@param inputStr 输入字符串
@return java.lang.String 返回统计结果
/
public String charCountStatistical(String inputStr){//以下注释假设 inputStr = "aabbbc"
/
int index = map.get(currentCharacter); // 查询统计数据存放位置
int count = resultList.get(index).getCount(); // 获取统计数据
resultList.get(index).setCount(count + 1); // 更新统计数据
}else { CharCount charCount = new CharCount(currentCharacter,1); // 之前没有统计过,新建一个
resultList.add(charCount); // 保存到结果中
map.put(currentCharacter,resultList.size()-1); // 记录统计结果存放位置
} // 创建一个自己的比较器对象
MyComparator myComparator = new MyComparator();
// 利用比较强排序
resultList.sort(myComparator);
// 遍历 resultList 获取输出结果 (使用迭代器遍历)
String resultStr = "";
Iterator iterator = resultList.iterator();
while (iterator.hasNext()){
CharCount next = iterator.next();
resultStr += next.getC() + " " + next.getCount() + " ";
}
// 去除结果最后面的空格
resultStr = resultStr.substring(0,resultStr.length() - 1);
return resultStr;
}
// 自建一个比较器,比较 CharCount , 根据 CharCount 的 count 成员升序
class MyComparator implements Comparator{
@Override
public int compare(CharCount o1, CharCount o2) {
return o1.getCount() - o2.getCount();
}
}
// 自建一个内部类,存放字符和数量
class CharCount{
char c;
int count;
public CharCount(char c, int count) {
this.c = c;
this.count = count;
}
public char getC() {
return c;
}
public void setC(char c) {
this.c = c;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
}
我感觉 第一题输出ascll码 输出字母最大的 再输出数字最大的 希望对你有个新的思路