当前位置:网站首页>给一个字符串 ① 请统计出其中每一个字母出现的次数② 请打印出字母次数最多的那一对
给一个字符串 ① 请统计出其中每一个字母出现的次数② 请打印出字母次数最多的那一对
2022-07-24 07:05:00 【每天都要交作业】
- 根据题目,首先想到了两个方法。一个是用哈希表,另一个就是用Map集合来存储<字符,个数>的键值对。
第一种方法:
public class StringTest {
/*设置一个静态的哈希数组,我们都知道每个字母都对应着一个askii码值,
将字母的askII码值当作下标,当遍历到它时,它的值就加一。
例:a的ascii码值为65,遍历到它时,将arr[65]++;*/
static int[] hash = new int[255];
public static void main(String[] args) {
String str = "ksjhfkjashfkahfkahfiuuytwurekahd";
//遍历字符串的每一个字符
for(int i = 0;i<str.length();i++){
char c = str.charAt(i);
hash[c]++;
}
int max = 0;
//找出出现次数的最大值
for(int i:hash){
if(i>max)
max = i;
}
//当hash数组中有值等于max,则打印它下标所对应的字符
for(int i = 0;i<hash.length;i++){
if(hash[i]==max){
System.out.println(Character.toChars(i));
}
}
}
}第二种方法:
public static void main(String[] args) {
String str = "ksjhfkjashfkahfkahfiuuytwurekahd";
HashMap<Character,Integer> hm = new HashMap<>();
Set<Character> charSet = hm.keySet();
/*遍历字符串,如果charSet有这个元素,则在hm里面将此元素的键值加1;
如果charSet没有这个元素,则将这个值作为键,并把它的值赋为1,添加进hm中*/
for(int i = 0;i<str.length();i++){
char c = str.charAt(i);
if(!charSet.contains(c)){
hm.put(c,1);
}else{
int num = hm.get(c);
num++;
hm.put(c,num);
}
}
//将hm的键值对的值设置在一个集合中并遍历找到最大值
int max = 0;
for(Integer i:hm.values()){
if(i>max)
max = i;
}
//遍历HashMap中的元素,将值等于max的键输出
Set<Map.Entry<Character, Integer>> sme = hm.entrySet();
for(Map.Entry<Character,Integer> me:sme){
if(me.getValue()==max){
System.out.println(me.getKey());
}
}
}
}
边栏推荐
- fopen、fwrite、fseek、ftell、fread使用demo
- What kind of mode can make platform users self-help fission- Chain 2+1
- [line test] Figure finding regular questions
- 文件上传下载Demo
- Compilation and debugging (GCC, g++, GDB)
- Redis fragment cluster
- Learning notes - distributed transaction theory
- avaScript的流程控制语句
- Take you to learn C step by step (second)
- Mongodb application scenario and model selection (massive data storage model selection)
猜你喜欢

Part II - C language improvement_ 3. Pointer reinforcement

RIoTBoard开发板系列笔记(九)—— buildroot 移植MatchBox

解压主播狂揽4000w+播放,快手美食赛道又添新风向?
![[line test] Figure finding regular questions](/img/61/d1c2cd399cf0d808e4fa25cd5fe681.png)
[line test] Figure finding regular questions

Traditional e-commerce dividends disappear, how to enter the new social e-commerce?
![[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled](/img/b0/54a826287154be5b758b3850e7fa51.png)
[steering wheel] the super favorite idea efficiency artifact save actions is uninstalled

What kind of mode can make platform users self-help fission- Chain 2+1

QoS quality of service 4 traffic regulation of QoS boundary behavior

Redis persistence

MITRE ATT&CK超详细学习笔记-01(背景,术语,案例)
随机推荐
2D目标检测综述之学习策略篇(终章)
C process running permission
Cmake notes
One book a day: machine learning and practice -- the road to the kaggle competition from scratch
yocs_ velocity_ Smooth source code compilation
Hackingtool of security tools
Blockbuster live broadcast | orb-slam3 series code explanation map points (topic 2)
单点登录的三种实现方式
Chapter007-FPGA学习之IIC总线EEPROM读取
Basic syntax of MySQL DDL and DML and DQL
ROS starts non native nodes
[word] how to generate the index directory on the left
深度学习二三事-回顾那些经典卷积神经网络
一个怎样的模式能让平台用户发生自助裂变?-链动2+1
JS_实现多行文本根据换行分隔成数组
Variables and data types (03)
JMeter笔记2 | JMeter原理及测试计划要素
Libevent and multithreading
GIMP自定义截图
Wild pointer, null pointer, invalid pointer