【问题标题】:Cannot read repeating characters无法读取重复字符
【发布时间】:2013-04-12 16:10:52
【问题描述】:

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

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 组重复小数 啊 | bb |啊啊啊bb

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

【问题讨论】:

  • 当你点击重复时保存字符,以便下次你可以检查它是否是相同的字符。并从 0 开始循环。
  • 啊,我试试看,我开始注意到我只会在字母的每一个变化中增加计数

标签: java string loops char


【解决方案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

【讨论】:

    【解决方案2】:

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

    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++;    
            }
        }
    }   
    

    【讨论】:

    • 啊,对于第二个 if 语句,您设置了 index = 0?不知道我能做到!
    • @qvd 我没有设置索引,这是为了避免 ArrayIndexOutOfBounds : (i-1) when i=0
    【解决方案3】:

    试试这个----

        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;
    }
    

    【讨论】:

    • 标志表示两个连续字符匹配与否。
    【解决方案4】:

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

    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);
    }
    

    【讨论】:

      【解决方案5】:

      你可以试试这样的:-

      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 和其他内容。

      【讨论】:

      • 非常感谢,这不适用于没有重复字母的字符串,但这很有帮助!
      • 会的!它会给1 作为重复计数。尝试一个字符串说 test 并让我知道它是否有效!
      【解决方案6】:

      使用 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;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-15
        • 2018-10-23
        • 2013-07-14
        相关资源
        最近更新 更多