0

元の文字列のバージョンを次のように返すメソッドを作成しようとしています。元の文字列に表示される各数字 0 ~ 9 は、数字の右側にある文字の出現回数に置き換えられます。したがって、文字列 "a3tx2z" は "attttxzzz" になり、"12x" は "2xxx" になります。文字が続かない数字 (つまり、文字列の末尾) は、何も置き換えられません。

私はコードを書きましたが、それは最初の桁だけで機能し、次の桁では変更されません。

public String blowUp( String str ){

        StringBuffer buffer = null;
        String toAdd = null;
        String toReturnString = null;

        if( str.length() == 0 ){

            return "no string found";
        }else{

            for( int count = 0; count < str.length(); count++ ){

                char c = str.charAt( count );

                if( count == str.length() - 1 ){

                    if( Character.isDigit( c ) ){

                        return str.substring( 0, count );
                    }else{

                        return str;
                    }
                }else if( Character.isDigit( c ) ){

                    char next = str.charAt( count + 1 );
                    buffer = new StringBuffer();
                    int nooftimes = Integer.parseInt(Character.toString( c ));

                    for( int j = 0; j < nooftimes; j++ ){

                        buffer.append( next );
                    }
                    toAdd = buffer.toString();
                    toReturnString = str.substring( 0, count ) + toAdd + str.substring( count + 1 );
                    return toReturnString;
                }
            }
        return toReturnString;
        }
    //  return toReturnString;
    }
4

4 に答える 4

1

コメントを参照してください。

public String blowUp(String str) {

        StringBuffer buffer = new StringBuffer();
        // String toAdd = null;
        // String toReturnString = null;

        if (str.length() == 0) {
            return "no string found";
        } else {

            for (int count = 0; count < str.length(); count++) {

                char c = str.charAt(count);
                /*
                 * if (count == str.length() - 1) {
                 * 
                 * if (Character.isDigit(c)) {
                 * 
                 * return str.substring(0, count); } else {
                 * 
                 * return str; } } else
                 */
                if (Character.isDigit(c) && count < str.length()-1) {

                    char next = str.charAt(count + 1);

                    if (!Character.isDigit(next)) { // append only if next
                                                    // character isn't digit

                        // buffer = new StringBuffer();
                        int nooftimes = Integer.parseInt(Character.toString(c));

                        for (int j = 0; j < nooftimes; j++) {

                            buffer.append(next);
                        }

                    } else {
                        buffer.append(str.charAt(count+1)); // append digit followed by another digit with next digit
                    }
                    // toAdd = buffer.toString();
                    // toReturnString = str.substring(0, count) + toAdd
                    // + str.substring(count + 1);
                } else {
                    buffer.append(c); // simply append if not digit
                }
            }
            return buffer.toString();
        }
        // return toReturnString;
    }
于 2013-10-03T03:40:49.347 に答える
0

これを必要以上に複雑にしているようで、その過程でロジックが少し混乱しています。

疑似コードでは、この問題は基本的に次のように要約されます。

for every character 'c' in the input, starting from the first:
    if c is not a digit:
        add c to the output
    otherwise (i.e. if c is a digit), if c is not the last character in the input:
        let 'x' be the number represented by c
        let 'n' be the next character in the input after c
        add x copies of n to the output
return the output

それを Java に翻訳するのはあなたに任せます* .


* 編集:または@TheKojuEffectへ!

于 2013-10-03T03:41:05.113 に答える
0

あなたのコードには 5 つ以上のreturnポイントがあります。これは本当に悪いことです。コードで何が起こっているのかを正確に判断することができなくなります。

私は古い学校なので、どの方法でも 1 つのエントリ ポイントと 1 つの出口ポイントがあると信じています。

始めましょう...

    if( str.length() == 0 ){
        return "no string found";

これは実際には元のString変更されていないものを返すはずです...そうではありませんか...

            if( count == str.length() - 1 ){
                if( Character.isDigit( c ) ){
                    return str.substring( 0, count );
                }else{
                    return str;
                }

なぜこれが必要なのかさえ理解できません。最後の文字は最初の文字ほど特別なものではありませんが、それが数字の場合は無視する必要があります...

                char next = str.charAt( count + 1 );
                buffer = new StringBuffer();
                int nooftimes = Integer.parseInt(Character.toString( c ));
                for( int j = 0; j < nooftimes; j++ ){
                    buffer.append( next );
                }
                toAdd = buffer.toString();
                toReturnString = str.substring( 0, count ) + toAdd + str.substring( count + 1 );
                return toReturnString;

では、returnこちらで!しかし、コードのこのセクションに入力したのは 1 回だけです。の残りはStringどうですか?

また、私が知る限り、あなたは他のすべてのキャラクターを無視しているようStringです...

より簡単なアイデアは、StringBuffer(またはStringBuilder私が好むように)を使用し、結果の値をそれに追加し続けることです。たとえば...

public static String blowUp(String str) {
    StringBuilder sb = new StringBuilder(128);
    for (int count = 0; count < str.length(); count++) {
        char c = str.charAt(count);
        if (Character.isDigit(c) && count < str.length() - 1) {
            char next = str.charAt(count + 1);
            int nooftimes = Integer.parseInt(Character.toString(c));
            for (int j = 0; j < nooftimes; j++) {
                sb.append(next);
            }
            count++;
        } else if (!Character.isDigit(c)) {
            sb.append(c);
        }
    }
    return sb.toString();
}

入出力例

入力したら…

a3tx2z    
12x    
a3tx2z1    
12x1    

私は得る

atttxzz
2x
atttxzz
2x

出力として

于 2013-10-03T03:44:59.037 に答える
0

@MadProgrammer あなたのコードに基づいて少し修正しました。

public String blowup(String str) {
    StringBuilder sb = new StringBuilder();
    for(int i =0; i < str.length(); i++) {
        char c = str.charAt(i);
        if(Character.isDigit(c) && i < str.length()-1) {
            char next = str.charAt(i+1);
            if(!Character.isDigit(next)) {
                int repTimes = Integer.parseInt(Character.toString(c));
                for(int k = 0; k < repTimes; k++) {
                    sb.append(next);
                }
            }
            else {
                sb.append(c);
            }
        }
        else {
            sb.append(c);
        }
    }
    return new String(sb);
}
于 2014-02-10T17:03:29.707 に答える