0

我正在编写一个代码来读取一个字符串并计算重复的集合

public int countRepeatedCharacters()
{
    int c = 0;
    for (int i = 1; i < word.length() - 1; i++)
    {
        if (word.charAt(i) == word.charAt(i + 1)) // found a repetition
        {
            if ( word.charAt(i - 1) != word.charAt(i)) {

                c++;

            }
        }
    }     
    return c;
}

如果我尝试输入 aabbcdaaaabb 我应该有 4 组重复小数 aa | bb | 啊啊啊 bb

而且我知道我没有读取第一组 aa,因为我的索引从 1 开始。我尝试将其修复为读取零,但随后我尝试修复整个循环以处理更改,但我失败了,有什么建议吗?如何更改我的索引或循环?

4

6 回答 6

1

试试这个代码:

public int countRepeatedCharacters(String word)
{
    int c = 0;
    Character last = null;
    bool counted = false;
    for (int i = 0; i < word.length(); i++)
    {
        if (last != null && last.equals(word.charAt(i))) {  // same as previous characted
            if (!counted) {  // if not counted this character yet, count it
                c++;
                counted = true;
            }
        }
        else {      // new char, so update last and reset counted to false
            last = word.charAt(i);
            counted = false
        }
    }     
    return c;
}

编辑 - 将 aaaa 计为 4,固定计为 1

于 2013-04-19T09:52:57.933 回答
1

试试这个方法。它计算重复字符的集合。

public static void main(String[] args) {
    String word = "aabbcdaaaabbc";
    int c = 0;
    for (int i = 0; i < word.length()-1; i++) {

        // found a repetition
        if (word.charAt(i) == word.charAt(i + 1)) {
            int k = 0;
            while((i + k + 1) < word.length()) {
                if(word.charAt(i+k) == word.charAt(i + k + 1)) {
                    k++;
                    continue;
                }
                else {
                    break;
                }
            }
            c++;
            i+=k-1;
        }
    }
    System.out.println(c);
}
于 2013-04-19T10:39:26.177 回答
1

根据我从您的问题中了解到的情况,您想计算重复集的数量,那么这应该会有所帮助。

for (int i = 0; i < word.length()-1; i++){
    if (word.charAt(i) == word.charAt(i + 1)){ // found a repetition
        if (i==0 || word.charAt(i - 1) != word.charAt(i)) {    
            c++;    
        }
    }
}   
于 2013-04-19T09:49:07.150 回答
1

尝试这个 - -

    public int countRepeatedCharacters()
{
    int c = 0,x=0;
    boolean charMatched=false;
    for (int i = 0; i < word.length(); i++)
    {
        if(i==word.length()-1)
        {
            if (word.charAt(i-1) == word.charAt(i))
                 c++;
            break;
        }
        if (word.charAt(i) == word.charAt(i + 1)) // found a repetition
        {

            charMatched=true;
            continue;
        }
        if(charMatched==true)
        c++;
        charMatched=false;          
    }     
    return c;
}
于 2013-04-19T10:04:37.390 回答
0

使用 length() -1 会导致您不考虑计算中的最后一个字符。这会导致您丢失最后一个重复字符。

最后,我会这样做:

 public static int countRepeatedCharacters(String word)
    {
        boolean withinRepeating = false;
        int c  = 0;

        for (int i = 1; i < word.length(); i++)
        {

            if (!withinRepeating && (withinRepeating = word.charAt(i) == word.charAt(i - 1)))
            c++;                        
            else
            withinRepeating = word.charAt(i) == word.charAt(i - 1);
        }     
        return c;
    }
于 2013-04-19T09:52:45.263 回答
0

你可以尝试这样的事情: -

public static void main(String str[]) {
    String word = "aabbcdaaaabbc";
    int c = 1;
    for (int i = 0; i < word.length() - 1; i++) {
        if (word.charAt(i) == word.charAt(i + 1)) {
            c++;
        } else {
            System.out.println(word.charAt(i)+ " = " +c);
            c = 1;
        }
    }
    System.out.println(word.charAt(word.length()-1)+ " = " +c);
}

您可以根据需要修改它,方法是删除sysouts和其他东西。

于 2013-04-19T09:49:20.950 回答