游程编码压缩

时间:2019-02-26 17:31:26

标签: java

因此,此代码的重点是压缩字符串,如果出现超过3次,则添加“#+出现次数”。对于字母,数字值的格式为(#num)(字母),它们都具有“#”。

例如,如果我有yyyy77777,输出将为#4y#5#7 但我的问题是它不会在其前面打印带有“#”的数字值,因此我将其输出为$ 4y#57

我遇到的另一个问题是,当它小于3时,它只会打印相同的内容。因此,即:nnnnnff的输出应为#5nff 但是我的代码将其变成了#5n2f

任何帮助将不胜感激!

public class Compress {

public static void main(String[] args) {
    java.util.Scanner in = new java.util.Scanner(System.in);
    String s = in.next();
    String compressString = compress(s);
    System.out.println(compressString);

}
public static String compress (String original){
    String str = "";
   // int n = (Integer) null;
    int count = 1;
    for(int i=0; i<original.length()-1; i++){

        if(original.charAt(i) == original.charAt(i+1)){
            count++;
            if(i == original.length()-2){
                if(count == 1){
                    str = str + original.charAt(i);
                }
                //if count is equal or greater than 3, print with #
                else if (count >= 3) {
                    str = str + "#" + count +  original.charAt(i);
                }          
                else{
                    str = str + original.charAt(i);
                }                    
            }
        }
        else{
            if(count == 1){
                if(i == original.length() - 2){
                    str = str + original.charAt(i);
                    str = str + original.charAt(i+1);
                }
                else{
                str = str + original.charAt(i);
                }
            }
            else{
                if(i == original.length() - 2){
                    str = str + count + original.charAt(i);
                    str = str + original.charAt(i+1);
                }
                else{
                    str = str +"#"+ count + original.charAt(i);
                }


            }

            count = 1;
        }
    }
    return str;
} 

}

1 个答案:

答案 0 :(得分:1)

您的代码当前正以完全相同的方式处理数字和非数字。要修复代码,只需添加if语句来检查字符是否为数字。

您可以在将#(count)(character)附加到结果之前添加if语句。

替换此行(有两行是这样!将它们都替换):

str = str + "#" + count +  original.charAt(i);

if (Character.isDigit(original.charAt(i))) {
    str = str + "#" + count + "#" + original.charAt(i);
} else {
    str = str + "#" + count +  original.charAt(i);
}

这是我解决这个问题的方法:

public static String compress (String original){
    String str = "";
    int count = 0;
    char currentChar = original.charAt(0);
    for(int i=0; i<original.length(); i++){
        if (currentChar == original.charAt(i)) { // count it if this not a new character
            currentChar = original.charAt(i);
            count++;
        } else { // if we have encountered a new character
            if (count > 2) { // and we have already counted 2 or more of the previous char
                if (Character.isDigit(currentChar)) {
                    str += "#" + count + "#" + currentChar;
                } else {
                    str += "#" + count + currentChar;
                }
            } else if (count == 2) { // if we have only counted two of the previous char
                str += currentChar;
                str += currentChar;
            } else if (count == 1) { // if we have only counted one of the previous char
                str += currentChar;
            }
            currentChar = original.charAt(i);
            count = 1;
        }
    }
    // treat the end of the string as a new char
    if (count > 2) {
        if (Character.isDigit(currentChar)) {
            str += "#" + count + "#" + currentChar;
        } else {
            str += "#" + count + currentChar;
        }
    } else if (count == 2) {
        str += currentChar;
        str += currentChar;
    } else if (count == 1) {
        str += currentChar;
    }
    return str;
}