What is the most efficient algorithm for reversing

2020-02-02 06:17发布

What is the most efficient way to reverse a string in Java? Should I use some sort of xor operator? The easy way would be to put all the chars in a stack and put them back into a string again but I doubt that's a very efficient way to do it.

And please do not tell me to use some built in function in Java. I am interested in learning how to do it not to use an efficient function but not knowing why it's efficient or how it's built up.

21条回答
Animai°情兽
2楼-- · 2020-02-02 07:02

One variant can be, swapping the elements.

int n = length - 1;
char []strArray = str.toCharArray();
for (int j = 0; j < n; j++) {
   char temp = strArray[j];
   char temp2 = strArray[n];
   strArray[j] = temp2;
   strArray[n] = temp;
   n--;
  }
查看更多
放我归山
3楼-- · 2020-02-02 07:02
public static void main(String[] args){
    String string ="abcdefghijklmnopqrstuvwxyz";
    StringBuilder sb = new StringBuilder(string);
    sb.reverse();

    System.out.println(sb);
}
查看更多
家丑人穷心不美
4楼-- · 2020-02-02 07:02
public static String Reverse(String word){
        String temp = "";
        char[] arr = word.toCharArray();
        for(int i = arr.length-1;i>=0;i--){
            temp = temp+arr[i];
        }
        return temp;
    }
查看更多
姐就是有狂的资本
5楼-- · 2020-02-02 07:03

The fastest way would be to use the reverse() method on the StringBuilder or StringBuffer classes :)

If you want to implement it yourself, you can get the character array, allocate a second character array and move the chars, in pseudo code this would be like:

String reverse(String str) {
    char[] c = str.getCharArray
    char[] r = new char[c.length];
    int    end = c.length - 1

    for (int n = 0; n <= end; n++) {
        r[n] = c[end - n];
    }

    return new String(r);
}

You could also run half the array length and swap the chars, the checks involved slow things down probably.

查看更多
做自己的国王
6楼-- · 2020-02-02 07:04

Of course this is the most efficient way:

String reversed = new StringBuilder(str).reverse().toString();

But if you don't like using that then I recommend this instead:

public String reverseString(String str)
{
    String output = "";
    int len = str.length();
    for(int k = 1; k <= str.length(); k++, len--)
    {
        output += str.substring(len-1,len);
    }
    return output;
}
查看更多
Evening l夕情丶
7楼-- · 2020-02-02 07:05
public static string getReverse(string str)
{
    char[] ch = str.ToCharArray();
    string reverse = "";
    for (int i = str.Length - 1; i > -1; i--)
    {
        reverse += ch[i];
    }
    return reverse;
}

//using in-built method reverse of Array
public static string getReverseUsingBulidingFunction(string str)
{
    char[] s = str.ToCharArray();
    Array.Reverse(s);
    return new string(s);
}


public static void Main(string[] args)
{
    string str = "123";
    Console.WriteLine("The reverse string of '{0}' is: {1}",str,getReverse(str));
    Console.WriteLine("The reverse string of '{0}' is: {1}", str, getReverseUsingBulidingFunction(str));
    Console.ReadLine();
}
查看更多
登录 后发表回答