Java中的字符串压缩算法
我希望实现一种方法,以执行以下形式的基本字符串压缩:Java中的字符串压缩算法,java,string,algorithm,Java,String,Algorithm,我希望实现一种方法,以执行以下形式的基本字符串压缩: aabcccccaaa -> a2b1c5a3 我有这个计划: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); System.out.printl
aabcccccaaa -> a2b1c5a3
我有这个计划:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(compress(str));
}
public static String compress(String str) {
char[] chars = str.toCharArray();
int count = 0;
String result = "";
for (int i = 0; i < chars.length; i++) {
char curr = chars[i];
result += curr;
for (int j = i; j < chars.length; j++) {
if (chars[j] == curr) {
count++;
}
else {
i += count;
break;
}
}
result += count;
count = 0;
}
return result;
}
}
import java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串str=sc.nextLine();
System.out.println(compress(str));
}
公共静态字符串压缩(字符串str){
char[]chars=str.toCharArray();
整数计数=0;
字符串结果=”;
for(int i=0;i
但在我的测试中,我总是错过最后一个字符计数
我假设这是因为程序在应该之前就退出了内部for循环,但为什么会这样呢
非常感谢您不需要两个for循环就可以这样一次完成
String str = "aaabbbbccccca";
char[] chars = str.toCharArray();
char currentChar = str.length() > 0 ? chars[0] : ' ';
char prevChar = ' ';
int count = 1;
StringBuilder finalString = new StringBuilder();
if(str.length() > 0)
for(int i = 1; i < chars.length; i++)
{
if(currentChar == chars[i])
{
count++;
}else{
finalString.append(currentChar + "" + count);
prevChar = currentChar;
currentChar = chars[i];
count = 1;
}
}
if(str.length() > 0 && prevChar != currentChar)
finalString.append(currentChar + "" + count);
System.out.println(finalString.toString());
String str=“aaabbbccccca”;
char[]chars=str.toCharArray();
char currentChar=str.length()>0?字符[0]:'';
char prevChar='';
整数计数=1;
StringBuilder finalString=新的StringBuilder();
如果(str.length()>0)
for(int i=1;i0&&prevChar!=currentChar)
finalString.append(currentChar+“”+count);
System.out.println(finalString.toString());
输出为:aaabbbccccca的a3b4c5a1跟踪您正在读取的字符,并将其与字符串的下一个字符进行比较。如果不同,则重置计数。
Keep a track of character that you are reading and compare it with next character of the string. If it is different, reset the count.
public static void stringCompression (String compression) {
String finalCompressedString = "";
char current = '1';
int count = 0;
compression = compression + '1';
for (int i = 0; i < compression.length(); i++) {
if (compression.charAt(i) == current) {
count = count + 1;
} else {
if (current != '1')
finalCompressedString = finalCompressedString + (current + Integer.toString(count));
count = 1;
current = compression.charAt(i);
}
}
System.out.println(finalCompressedString);
}
公共静态void stringCompression(字符串压缩){
字符串finalCompressedString=“”;
字符电流='1';
整数计数=0;
压缩=压缩+1';
对于(int i=0;i
我对java中字符串压缩的回答。
在这里,我所做的是,你应该做的是,记录特定次数的字符,通过比较当前字符和下一个字符,当当前字符和下一个字符变得不相等时,重置计数值,并对下一个不同的字符重复整个过程。
希望有帮助
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int count = 0;
for (int i=0; i<str.length(); ++i) {
int j=i+1;
count=1;
while (j!=str.length() && str.charAt(i) == str.charAt(j)) {
count += 1;
j += 1;
i += 1;
}
System.out.print(str.charAt(i));
if (count > 1) {
System.out.print(count);
}
}
}
}
import java.util.*;
公共班机{
公共静态void main(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
字符串str=sc.nextLine();
整数计数=0;
对于(int i=0;i 1){
系统输出打印(计数);
}
}
}
}
是的,您需要跟踪最后遇到的字符,当您退出for循环时,将a3添加到字符串中使用aaabbccc运行它会生成a3b2c2c1。你需要重新考虑你的算法。特别是,关于增量i的方式。不要构建可能很长的字符串
s“添加”到字符串
类型变量的当前不可变值:使用。试着只使用一次迭代。解决这类问题的一个很好的方法是使用一个调试程序,听起来像是“一个错误就搞定了”。您应该使用调试器来帮助跟踪确切的问题。调试技巧对任何程序员来说都是非常重要的。事实上,它们比知道如何编写代码更重要。(不要构建可能很长的String
s“添加”到String
类型的当前不可变值中:使用StringBuilder.append()
)这对有用吗“按1”
?感谢您改进了答案。但是我还是不喜欢,因为你的代码比原来的代码差。方法名main
没有恰当地描述它正在做什么,应该无条件地追加count
。代码的格式看起来很不规范。为什么不让IDE为您格式化代码呢?