只需刷新一些旧的java技术。目前正在解决一系列问题,并且这个问题将压缩字符串,格式为aabbcccDDDDeff到a2b2c3d4e1f2。我的代码中发生了一些奇怪的事情,请帮助对其进行分类:将字符串压缩成a2b3 ...等
public static void main(String[] args) {
String c = "aabbCCCCCdfff";
System.out.println(compress(c));
}
public static String compress(String s) {
String ns = "";
int count = 0;
char temp = 0;
for (int x = 0; x < s.length(); x++) {
if (x == 0) {
ns.concat(String.valueOf(s.charAt(x)));
temp = s.charAt(x);
count++;
} else if (temp == s.charAt(x)) {
count++;
} else {
ns.concat(String.valueOf(count));
count = 0;
ns.concat(String.valueOf(s.charAt(x)));
temp = s.charAt(x);
}
}
return ns;
}
输出显示为空。我想继续我的同样的逻辑
完美。哈哈。非常感谢!代码仍然搞砸了一点,但这是我需要的 – erp 2015-01-31 18:42:14