【问题标题】:Shifting characters within a string在字符串中移动字符
【发布时间】:2013-12-13 16:03:22
【问题描述】:
String newStr;

public RandomCuriosity(String input){
    newStr = input;
}

public void shiftChars(){
    char[] oldChar = newStr.toCharArray();
    char[] newChar = new char[oldChar.length];
    newChar[0] = oldChar[oldChar.length-1];
    for(int i = 1; i < oldChar.length; i++){
        newChar[i] = oldChar[i-1];
    }
    newStr = String.valueOf(newChar);
}

我创建了一种将字符向前移动一个的方法。例如,输入可能是:

输入:Stackoverflow

输出:wStackoverflo

我是怎么做到的,我改变了一个字符串的实例。将该字符串转换为char 数组(称其为oldChar),将oldChar 的最后一个索引分配为newChar 的第一个索引,并创建一个取oldChar 的第一个索引的for 循环作为我的新 Char 数组的第二个索引,依此类推。最后,我将 char 数组转换回字符串。

我觉得我做的事情太多了,非常简单。有没有更有效的方法来做这样的事情?

编辑感谢您的精彩回答!

【问题讨论】:

  • 你的程序的时间和空间复杂度是多少?
  • 我不知道,虽然我认为它相当高。
  • 这似乎是题外话,因为它应该在:codereview.stackexchange.com
  • @SotiriosDelimanolis 子字符串解决方案实际上更快吗?我不知道子字符串是否产生 O(1)?如果我没记错的话,较新版本的 java 不再保留偏移量。
  • 作为术语的旁注:您没有改变 String 的实例。您创建了一个具有相似但更改了值的新字符串。字符串是不可变的。

标签: java arrays string char


【解决方案1】:
newStr = newStr.charAt(newStr.length() - 1) + newStr.substring(0, newStr.length() - 1);

【讨论】:

  • 哇。这是一个强烈的单线。所以让我直截了当。您将最后一个索引视为一个字符,并将最后一个索引之前的字符视为一个完整的字符串?
  • 是的。但是如果你觉得更清楚,你也可以使用newStr.substring(newStr.length() - 1)作为第一个操作数。
  • 这是一个非常漂亮的解决方案,并且很好地使用了内置的 Java 方法。谢谢JB。
  • @JB 你能检查我对这个问题的最后评论并评论我对复杂性的看法是否正确吗?
  • 只是更正代码行。它在第二个newStr.length 中缺少()。将是:newStr = newStr.charAt(newStr.length() - 1) + newStr.substring(0, newStr.length() - 1);
【解决方案2】:

你可以让你的生活更简单:

public static void main (String[] args) throws java.lang.Exception {
    String input = "Stackoverflow";
    for(int i = 0; i < s.length(); i++){
        input = shift(input);
        System.out.println(input);
    }
}

public static String shift(String s) {
    return s.charAt(s.length()-1)+s.substring(0, s.length()-1);
}

输出:

wStackoverflo
owStackoverfl
lowStackoverf
flowStackover
rflowStackove
erflowStackov
verflowStacko
overflowStack
koverflowStac
ckoverflowSta
ackoverflowSt
tackoverflowS
Stackoverflow

【讨论】:

    【解决方案3】:

    你可以使用System.arrayCopy:

    char[] oldChar = newStr.toCharArray();
    char[] newChar = new char[oldChar.length];
    newChar[0] = oldChar[oldChar.length - 1];
    System.arrayCopy(oldChar, 0, newChar, 1, oldChar.length - 1);
    

    【讨论】:

      【解决方案4】:

      您可以使用 StringBuilders。

      StringBuilder strb = new StringBuilder();
      strb.append(oldChar[oldChar.length-1]).append(oldchar.substring(0, oldChar.length-1));
      newStr = strb.toString();
      

      【讨论】:

        【解决方案5】:

        试试这个..

                String old = "String";
                char first = old.charAt(old.length()-1);
                String newString = first+old.substring(0,old.length()-1);
                System.out.println(newString);
        

        【讨论】:

          【解决方案6】:

          另一种解决方案,但不使用循环,用于左移和右移:

          public static String cyclicLeftShift(String s, int n){ //'n' is the number of characters to shift left
                  n = n%s.length();
                  return s.substring(n) + s.substring(0, n);
              }
                
          public static String cyclicRightShift(String s, int n){ //'n' is the number of characters to shift right
                  n = n%s.length();
                  return  s.substring(s.length() - n , s.length()) + s.substring(0, s.length() - n);
              }
          

          【讨论】:

            【解决方案7】:

            通过Java,你可以将它向前移动O(n),其中n是按空格o(1)的字符向前移动的次数

            public static String shiftChars(String s , int times) {
                String temp = s;
                for (int i = 0; i < times ; i++) {
                    temp =  temp.charAt(temp.length()-1)+temp.substring(0, temp.length()-1);
                }
                return temp;
            }
            

            【讨论】:

            • 您的解决方案与 JB Nizet 接受的答案相同
            • 没关系。其实我只是想分享一下我是如何使用这段代码来解决我的问题的,谢谢你对代码的关注^_^
            • 您的回答可能会被删除。我们的想法是发布不同的答案。
            猜你喜欢
            • 2019-04-26
            • 2021-09-19
            • 1970-01-01
            • 2022-01-13
            • 1970-01-01
            • 1970-01-01
            • 2022-11-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多