Java 使用递归计算字符串中hi的出现次数
我遇到了这个问题: 给定一个字符串,递归计算(无循环)字符串中出现小写“hi”的次数 我运行我的代码,它工作得很好,但有没有更好的方法呢? 这是我的代码(提前谢谢):Java 使用递归计算字符串中hi的出现次数,java,recursion,Java,Recursion,我遇到了这个问题: 给定一个字符串,递归计算(无循环)字符串中出现小写“hi”的次数 我运行我的代码,它工作得很好,但有没有更好的方法呢? 这是我的代码(提前谢谢): 您不需要count参数,此外,当String的最后两个字符等于“hi”时,您可以通过删除它们来减少递归调用的数量: public int countHi(字符串){ if(string.length()=strLength){ 返回计数; }否则{ 如果(string.charAt(strCount)='h'&&(strCount
您不需要
count
参数,此外,当String
的最后两个字符等于“hi”时,您可以通过删除它们来减少递归调用的数量:
public int countHi(字符串){
if(string.length()<2){
返回0;//没有出现“hi”
}否则{
if(string.endsWith(“hi”)){
//一个引用+第一个长度为2个字符的子字符串中的引用
返回1+countHi(string.substring(0,string.length()-2));
}否则{
//第一个长度为1个字符的子字符串中的引用
返回countHi(string.substring(0,string.length()-1));
}
}
}
如果您不想使用字符串函数。这将是一个理想的代码
static int countRecursively(String string, int strCount, int count) {
int strLength = string.length();
if (strCount >= strLength) {
return count;
} else {
if (string.charAt(strCount) == 'h' && (strCount + 1) < strLength && string.charAt(strCount + 1) == 'i') {
count++;
return countRecursively(string, strCount + 2, count);
} else {
return countRecursively(string, strCount + 1, count);
}
}
}
递归静态整数计数(字符串、整数strCount、整数计数){
int strLength=string.length();
如果(strCount>=strLength){
返回计数;
}否则{
如果(string.charAt(strCount)='h'&&(strCount+1)
如果您想在单个函数中执行此操作,以下方法可以奏效
public int countHi(String str) {
int len = str.length();
if(len < 3)
{
if(len > 1 && str.substring(0,2).equals("hi"))
{
return 1;
}
return 0;
}
int counter = 0;
if(str.substring(len - 2,len).equals("hi"))
{
counter = 1;
}
return countHi(str.substring(0,len - 1)) + counter;
}
public int countHi(字符串str){
int len=str.length();
if(len<3)
{
if(len>1&&str.substring(0,2).equals(“hi”))
{
返回1;
}
返回0;
}
int计数器=0;
if(str.substring(len-2,len).equals(“hi”))
{
计数器=1;
}
返回countHi(str.substring(0,len-1))+计数器;
}
这种问题最好在问,因为“hi”
有两个字符的长度,可以提前一步完成处理:string.length()==1
您是说更好的方式(不管这意味着什么)但仍然是递归的吗?还是仍然没有循环?返回字符串.length()>=aString.length()?(string.startsWith(aString)?1:0)+countAString(string.substring(1,string.length()):0;
public int countHi(String string) {
if(string.length() < 2) {
return 0; // no occurrences of "hi"
} else {
if(string.endsWith("hi")) {
// one occurrence + the occurrences in the substring of the first length-2 chars
return 1 + countHi(string.substring(0, string.length()-2));
} else {
// the occurrences in the substring of the first length-1 chars
return countHi(string.substring(0, string.length()-1));
}
}
}
static int countRecursively(String string, int strCount, int count) {
int strLength = string.length();
if (strCount >= strLength) {
return count;
} else {
if (string.charAt(strCount) == 'h' && (strCount + 1) < strLength && string.charAt(strCount + 1) == 'i') {
count++;
return countRecursively(string, strCount + 2, count);
} else {
return countRecursively(string, strCount + 1, count);
}
}
}
public int countHi(String str) {
int len = str.length();
if(len < 3)
{
if(len > 1 && str.substring(0,2).equals("hi"))
{
return 1;
}
return 0;
}
int counter = 0;
if(str.substring(len - 2,len).equals("hi"))
{
counter = 1;
}
return countHi(str.substring(0,len - 1)) + counter;
}