当前位置:网站首页>字符串的相关编程题
字符串的相关编程题
2022-07-06 18:09:00 【胡阳阳Y】
字符串出现的次数
字符串A,另一个字符串B,计算B字符串在A字符串中出现几次
例子 : dsabdnabdsnabeabiwpabekabd **ab**
- 实现过程
- 对字符串进行索引查找 indexOf
- 找到的字符串的索引记录,进行字符串的截取
- 直到找打到未知, indexOf方法是-1
- 一旦找到了,计数器++
/**
* @param str 原始字符串
* @param sub 要查找的字符串
* @return 出现次数
*/
public static int stringCount(String str ,String sub){
//定义变量,计数器
int count = 0;
//定义变量,记录字符串查找后的索引
int index = 0;
//对字符串出现的位置,进行查询
//反复查找,使用循环while
//循环条件就是indexOf方法返回-1
while ( (index=str.indexOf(sub)) != -1 ) {
//执行了循环index !=-1 字符串出现了
count ++;
//截取字符串,开始索引 index+被查找字符串的长度
str = str.substring(index + sub.length());
}
return count;
}哪个字符出现的最多
要求 : 指定字符串自能是(小写)字母 abeegewff , 计算出哪个字符出现的次数最多
限定字符串中字母只能有26个
> 找每个字符各自出现多少次,找出最大值
- 实现过程 :
- 字符串转成数组 (单个字符操作)
- 创建长度为26的数组,计数器使用
- 取出数组中的字符, (字符-97)对应数组的索引,计数器++
- 找出数组中的最大值
/**
* 查找字符串中,哪个字符出现的次数最多
* @param str 要查找字符串
* @return 返回出现次数最多的字符
*/
public static char charCount(String str){
//字符串转成数组
char[] chars = str.toCharArray();
//定义26长度的数组,保存每个字符出现的次数
int[] count = new int[26];
//遍历数组
for (int i = 0 ; i < chars.length; i++){
//取出单个字符
char ch = chars[i];
//字符 - 97 作为数组的索引使用 (数组,计数器数组)
count[ ch - 97 ] ++;
}
//System.out.println("Arrays.toString(count) = " + Arrays.toString(count));
//取出count数组中的,最大值的索引
int index = 0 ; //数组最大值索引
int max = count[0];
for(int i = 1 ; i < count.length ; i++){
if (max < count[i]){
index = i;
max = count[i];
}
}
//index索引,正好和字符相差97
return (char) (index+97);
}
边栏推荐
- docker 方法安装mysql
- [chip scheme design] pulse oximeter
- Vocabulary in Data Book
- 736. LISP syntax parsing: DFS simulation questions
- What does front-end processor mean? What is the main function? What is the difference with fortress machine?
- Google released a security update to fix 0 days that have been used in chrome
- Installation of gazebo & connection with ROS
- 分享一个通用的so动态库的编译方法
- 图片打水印 缩放 和一个输入流的转换
- 百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)
猜你喜欢

Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which

从底层结构开始学习FPGA----FIFO IP的定制与测试

MySQL script batch queries all tables containing specified field types in the database

According to the analysis of the Internet industry in 2022, how to choose a suitable position?

Let's see how to realize BP neural network in Matlab toolbox
![JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]](/img/40/da56fe6468da64dd37d6b5b0082206.png)
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]

永久的摇篮

子网划分、构造超网 典型题

鼠标右键 自定义

454-百度面经1
随机推荐
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
dvajs的基础介绍及使用
黑马笔记---创建不可变集合与Stream流
Google发布安全更新,修复Chrome中已被利用的0 day
docker 方法安装mysql
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
制作带照明的DIY焊接排烟器
子网划分、构造超网 典型题
[chip scheme design] pulse oximeter
Transplant DAC chip mcp4725 to nuc980
Dark horse notes - exception handling
Instructions for using the domain analysis tool bloodhound
Right mouse button customization
2022 Google CTF SEGFAULT LABYRINTH wp
Yunna - work order management system and process, work order management specification
永久的摇篮
2022 Google CTF SEGFAULT LABYRINTH wp
1123. 最深叶节点的最近公共祖先
AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)