Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 确定字符的最大发生次数并返回起始索引_Java_String_Loops_Repeat_Charat - Fatal编程技术网

Java 确定字符的最大发生次数并返回起始索引

Java 确定字符的最大发生次数并返回起始索引,java,string,loops,repeat,charat,Java,String,Loops,Repeat,Charat,这是我的密码 给我找到结束这一切的方法。 我在论文中有一个问题,这是当时我能做的代码。 在下面的示例中,它应该返回3.(起始点为“d”) 公共类HelloWorld{ 公共静态void main(字符串[]args){ int result=getMax(“haaddccf”); 系统输出打印项次(结果); } 公共静态int getMax(字符串输入){ int length=input.length(); 整数计数=0; int max=0; int tempCount=0; for(int

这是我的密码

给我找到结束这一切的方法。 我在论文中有一个问题,这是当时我能做的代码。 在下面的示例中,它应该返回3.(起始点为“d”)

公共类HelloWorld{
公共静态void main(字符串[]args){
int result=getMax(“haaddccf”);
系统输出打印项次(结果);
}
公共静态int getMax(字符串输入){
int length=input.length();
整数计数=0;
int max=0;
int tempCount=0;
for(int i=0;i tempCount){
max=tempCount;
返回i;
}
tempCount=0;
}
返回0;
}
}

像这样的东西怎么样:

public class HelloWorld {

    public static void main(String []args){
        int result = getMax("haaddddddddccf");
        System.out.println(result);
    }

    public static int getMax(String input){
        int length = input.length();

        int maxIndex  = 0;
        int max       = 0;
        int count     = 1;
        char current  = input.charAt(0);
        int index = 0;

        for(int i=1; i<length-1; i++) {
            if(input.charAt(i) == current) {
                count ++ ;
                if(count > max) {
                    max = count;
                    maxIndex = index;
                }
            }
            else {
                count = 1;
                current = input.charAt(i);
                index = i;
            }                
        }
        return maxIndex;
    }
}
公共类HelloWorld{
公共静态void main(字符串[]args){
int result=getMax(“haaddccf”);
系统输出打印项次(结果);
}
公共静态int getMax(字符串输入){
int length=input.length();
int maxIndex=0;
int max=0;
整数计数=1;
字符电流=输入字符(0);
int指数=0;
对于(int i=1;i max){
最大值=计数;
maxIndex=索引;
}
}
否则{
计数=1;
电流=输入字符(i);
指数=i;
}                
}
返回最大索引;
}
}

这将覆盖整个字符串并计算连续出现的字符数。如果计数超过观察到的最大值,则保存该系列连续字符的开始索引以及字符数。如果字符发生变化,当前值将重置并重新开始计数。浏览完整个列表后,最长的连续字符序列的索引位于
maxIndex

类似的内容如何:

public class HelloWorld {

    public static void main(String []args){
        int result = getMax("haaddddddddccf");
        System.out.println(result);
    }

    public static int getMax(String input){
        int length = input.length();

        int maxIndex  = 0;
        int max       = 0;
        int count     = 1;
        char current  = input.charAt(0);
        int index = 0;

        for(int i=1; i<length-1; i++) {
            if(input.charAt(i) == current) {
                count ++ ;
                if(count > max) {
                    max = count;
                    maxIndex = index;
                }
            }
            else {
                count = 1;
                current = input.charAt(i);
                index = i;
            }                
        }
        return maxIndex;
    }
}
公共类HelloWorld{
公共静态void main(字符串[]args){
int result=getMax(“haaddccf”);
系统输出打印项次(结果);
}
公共静态int getMax(字符串输入){
int length=input.length();
int maxIndex=0;
int max=0;
整数计数=1;
字符电流=输入字符(0);
int指数=0;
对于(int i=1;i max){
最大值=计数;
maxIndex=索引;
}
}
否则{
计数=1;
电流=输入字符(i);
指数=i;
}                
}
返回最大索引;
}
}

这将覆盖整个字符串并计算连续出现的字符数。如果计数超过观察到的最大值,则保存该系列连续字符的开始索引以及字符数。如果字符发生变化,当前值将重置并重新开始计数。浏览整个列表后,最长连续字符序列的索引位于
maxIndex

str.indexOf()
返回第一个索引。是!谢谢。干杯:)
str.indexOf()
返回第一个索引。是的!谢谢。干杯:)