2

这是我的代码。

public static void countContinuosOccurence() {
    String first = "ABBCDDDEFGGH";
    StringBuffer result = new StringBuffer();
    int count = 1;
    for (int i = 1; i < first.length(); i++) {
        if (first.charAt(i) == (first.charAt(i - 1))) {
            count++;
        } else {
            if (count > 1) {

                result.append(String.valueOf(count) + first.charAt(i - 1));
            } else {
                result.append(first.charAt(i - 1));
            }
            count = 1;
        }
    }
    System.out.println("First String is:"+ first);
    System.out.println("Result is:" + result);
}

结果是:

First String is:ABBCDDDEFGGH
Result is:A2BC3DEF2G

它缺少最后一个字符?有人可以帮我解决这个问题吗?

4

5 回答 5

3

循环结束后for,您需要将计数和最后一次运行字符的字符附加到结果中:

public static void countContinuosOccurence() {
    String first = "ABBCDDDEFGGH";
    StringBuffer result = new StringBuffer();
    int count = 1;
    int i;
    for (i = 1; i < first.length(); i++) {
        if (first.charAt(i) == (first.charAt(i - 1))) {
            count++;
        } else {
            if (count > 1) {    
                result.append(String.valueOf(count) + first.charAt(i - 1));
            } else {
                result.append(first.charAt(i - 1));
            }
            count = 1;
        }
    }

    // ADD THIS - to take care of the last run.
    if (count > 1) {    
        result.append(String.valueOf(count) + first.charAt(i - 1));
    } else {
        result.append(first.charAt(i - 1));
    }

    System.out.println("First String is:"+ first);
    System.out.println("Result is:" + result);
}
于 2012-04-27T07:15:32.130 回答
3

不是性能最好,但最简单的代码:

final String in = "ABBCDDDEFGGH" + '\u0000';
final StringBuilder b = new StringBuilder();
char prev = in.charAt(0);
int rpt = 0;
for (int i = 1; i < in.length(); i++) {
  final char curr = in.charAt(i);
  if (curr == prev) rpt++;
  else {
    b.append(rpt == 0? prev : "" + (rpt + 1) + prev);
    rpt = 0; prev = curr;
  }
}
System.out.println(b);
于 2012-04-27T08:03:27.910 回答
1
public static void countContinuosOccurence() {

    String[] input = "ABBCDDDEFGGH".split("");
        String out = "";
        for (int i = 0; i < input.length; i++) {
            int repeatedCharCount = 1;
            String currentChr = input[i];
            if (!(i == input.length - 1)) {
                while (input[i].equals(input[i + 1])) {
                    repeatedCharCount++;
                    i++;
                }
            }
            out = out + repeatedCharCount + currentChr;
        }

        System.out.println(out);

}
于 2018-02-07T23:16:56.553 回答
0

还有一个隐藏的问题,那就是如果你以不止一次出现的序列终止,你就不会写任何东西。

解决此问题的最简单方法以及您检测到的问题是在 for 块之后添加最终检查

[...]
    }
    int l = first.length();
    if (count > 1) {
        result.append(String.valueOf(count) + first.charAt(l - 1));
        } else {
            result.append(first.charAt(l - 1));
        }
    }
    System.out.println("First String is:"+ first);
    System.out.println("Result is:" + result);
}
于 2012-04-27T07:26:08.000 回答
0
import java.util.*;
public class HelloWorld{
public static void main(String []args){
    System.out.println("Hello World");
    String first = "ABBCDDDEFGGHhhhhh456456456{{{67}}}";
StringBuffer result = new StringBuffer();
result.append(first);
System.out.println(result);
    Map<Character,Integer> map = new HashMap<Character,Integer>();
for(int i = 0; i < first.length(); i++) {
char c = first.charAt(i);
if (map.containsKey(c)) {
int cnt = map.get(c);
map.put(c, ++cnt);
} else {
map.put(c, 1);
}

}
Set set = map.entrySet(); 
// Get an iterator 
Iterator itr = set.iterator(); 
// Display elements 
while(itr.hasNext()) { 
Map.Entry me = (Map.Entry)itr.next(); 
System.out.print(me.getKey() + ": "); 
System.out.println(me.getValue()); 
} 
System.out.println("Hello World1");
 }
}
于 2013-05-20T18:33:35.527 回答