反向剩余

huangapple go评论68阅读模式
英文:

Reverse leftover

问题

我正在处理一个程序,该程序读取一串字符,并根据一个数字对某些字符进行反转。
(例如,给定“TELLER”和3,应返回“LETREL”)
我遇到的问题是,我需要反转剩下的额外字符。
(例如,给定“HELLO”和3,它返回“LEHLO”,但我需要它返回“LEHOL”)
以下是我当前尝试的方法:

for (int i = 0; i < st.length();) {
    if (i + x > st.length()-1){
        break;
    }

    st = st.substring(0, i) + new String(new StringBuilder(st.substring(i, i + x)).reverse()) + st.substring(i + x);

    i += x; }
英文:

I am working on a program that reads a string of characters and reverses certain characters based off a number.
(i.e. "TELLER" and 3 is given, it should return "LETREL)
The problem I am running into is that I need to reverse the extra characters left over
(i.e. "HELLO" and 3 is given, it returns "LEHLO" when I need it to return "LEHOL")
This is how i'm currently trying to go about it

for (int i = 0; i &lt; st.length();) {
            if (i + x &gt; st.length()-1){
                break;
            }

            st = st.substring(0, i) + new String(new StringBuilder(st.substring(i, i + x)).reverse()) + st.substring(i + x);

            i += x; }

答案1

得分: 2

String input = "Hello";
StringBuilder buffer = new StringBuilder();
int x = 3;

for (int d = 0; d < Math.floor(input.length() / x); d++) {
    buffer.append(new StringBuilder(input.substring(d * x, (d + 1) * x)).reverse());
}

if (input.length() % x != 0)
    buffer.append(new StringBuilder(input.substring(input.length() - input.length() % x - 1)).reverse());

System.out.println("Result is " + buffer.toString());
英文:
String input = &quot;Hello&quot;;
StringBuilder buffer = new StringBuilder();
int x = 3;

for(int d = 0; d &lt; Math.floor(input.length / x); d++){
    buffer.append((new StringBuilder(input.substring(d * x, (d + 1) * x))).reverse());
}

if(input.length % x != 0) buffer.append((new StringBuilder(input.substring(input.length - input.length % x - 1))).reverse());

System.out.println(&quot;Result is &quot; + buffer.toString());

答案2

得分: 0

尝试以下内容。应该可以正常工作。

for (int i = 0; i < st.length();) {
    if (i + x > st.length() - 1){
        s += new String(new StringBuilder(st.substring(i, st.length())).reverse());
        break;
    }

    s += new String(new StringBuilder(st.substring(i, i + x)).reverse());

    i += x;
}

如果有任何部分你不理解,请告诉我。祝编码愉快!

英文:

Try the following. It should work.

for (int i = 0; i &lt; st.length();) {
        if (i + x &gt; st.length()-1){
            s += new String(new StringBuilder(st.substring(i, st.length())).reverse());
            break;
        }

        s += new String(new StringBuilder(st.substring(i, i + x)).reverse());

        i += x;
    }

Let me know if you don't understand any part. Happy coding!

答案3

得分: 0

// 字符串拼接,+ 和 +=,会创建多个字符串对象。
// 同时,按给定步长遍历字符串会更容易:

StringBuilder reversed = new StringBuilder(st.length);
for (int i = 0; i + x <= st.length(); i += x) {
    int nextI = Math.min(i + x, st.length());
    for (int j = i; j < nextI; ++j) {
        reversed.insert(i, st.charAt(j));
    }
}
s = reversed.toString();

或者

for (int i = 0; i < st.length(); ) {
    int nextI = Math.min(i + x, st.length());
    for (int j = i; j < nextI; ++j) {
        reversed.insert(i, st.charAt(j));
    }
    i = nextI;
}
英文:

String concatenation, + and +=, is expensive as several String-s are created.
Also it would be easier to step through the string in the given step size:

StringBuilder reversed = new StringBuilder(st.length);
for (int i = 0; i + x &lt;= st.length(); i += x) {
    int nextI = Math.min(i + x, st.length());
    for (int j = i; j &lt; nextI; ++j) {
        reversed.insert(i, st.charAt(j));
    }
}
s = reversed.toString();

Or

for (int i = 0; i &lt; st.length(); ) {
    int nextI = Math.min(i + x, st.length());
    for (int j = i; j &lt; nextI; ++j) {
        reversed.insert(i, st.charAt(j));
    }
    i = nextI;
}

huangapple
  • 本文由 发表于 2020年10月19日 16:32:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64423753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定