压缩给定字符串,用重复出现的字符替换它们出现的次数


public class Test12CompressString {
public static String getCompressedString(String str) {
String newString = "";
int count = 1;
int len = str.length()-1;
for (int i = 0; i <= len ; i++) {
if(i != len) {
System.out.println(i);
if(str.charAt(i) == str.charAt(i+1)) {
count++;
continue;
} 
if(count == 1) {
newString = newString+str.charAt(i);
} else {
newString = newString+str.charAt(i)+count;
}
if ( str.charAt(i) != str.charAt(i+1)) {
count = 1;
continue;
}
} 
}
return newString;
}
public static void main(String[] args) {
String str = "abbbccccc";
String ans = getCompressedString(str);
System.out.print(ans);
}
}

期望输出:ab3c4

输出我得到:ab3

有人能告诉我缺少什么,为什么最后一个字符和它的计数从我的输出中丢失?有人能纠正我的代码吗?

当for循环结束时,代码只返回newString。代码添加'4'的唯一地方是当循环命中一个新字符并识别出这不再是c时。最后,在return newString之前,检查count是否超过1,如果超过,打印count

如上面注释所示,当循环结束字符串时,最后一个符号的结果将被丢弃。

我提供的方法与您的方法大致相同,但有一些改进。

  1. 如果null被传递给你的方法怎么办?您的代码将例外地失败。在我看来,在这种情况下返回null是有意义的。
  2. 如果参数字符串是空的,换句话说,它的长度是零?这种情况必须单独考虑。
  3. Java中的String类是不可变的。因此,字符串的每次连接都会创建一个String对象的新实例。要在这种情况下构建结果,最好使用StringBuilder.
  4. 在您的方法中,您在沿着字符串运行时向前看。将当前符号与下面的符号进行比较。要做到这一点,需要仔细观察index是否超过字符串的长度。我建议简化这一点,根本不向前看。
  5. 这并不漂亮,你可能有更好的方法。但是在循环结束后,我们需要在字符串中添加最后一个符号。这是一段重复的代码,所以我把它放在一个单独的函数中。
private static String getCompressedString(String str) {
if (Objects.isNull(str)) return null;
if (str.isEmpty()) return "";
StringBuilder result = new StringBuilder();
char calculated = str.charAt(0);
int repeats = 0;
for (char observed : str.toCharArray()) {
if (observed == calculated) {
repeats++;
} else {
append(result, calculated, repeats);
calculated = observed;
repeats = 1;
}
}
append(result, calculated, repeats);
return result.toString();
}
private static void append(StringBuilder result, char calculated, int repeats) {
result.append(calculated);
if (repeats > 1) {
result.append(repeats);
}
}

我完全相信有一种方法可以使这段代码更好、更短。我希望它能作为你的一个起点。

public static String getCompressedString(String str) {
String newString = "";
int count = 1;
int len = str.length() - 1;
for (int i = 0; i < len; i++) {
if (str.charAt(i) == str.charAt(i + 1)) {
count++;
continue;
}
if (count == 1) {
newString = newString + str.charAt(i);
} else {
newString = newString + str.charAt(i) + count;
}
if (str.charAt(i) != str.charAt(i + 1)) {
count = 1;
continue;
}
}
if (count == 1) {
newString = newString + str.charAt(len);
} else {
newString = newString + str.charAt(len) + count;
}
return newString;
}
public class CompressString {
public static void main(String[] args) {
String str = "abbbccccc";
String ans = getCompressedString(str);
System.out.println("String is --->" + ans);
}

public static String getCompressedString(String str) {
if (str.length() == 0) {
return "";
}
String newString = "";
int count = 0;
int len = str.length() - 1;
char firstVar = str.charAt(0);
for (int i = 1; i <= len; i++) {
if (firstVar != str.charAt(i)) {
if (count > 0) {
newString = newString + firstVar + count;
} else {
newString = newString + firstVar;
}
count = 0;
firstVar = str.charAt(i);
} else {
count++;
}
}
if (count > 0) {
newString = newString + firstVar + count;
} else {
newString = newString + firstVar;
}
return newString;
} }

请检查,这可能对你有帮助。

最新更新