LeetCode-344.Reverse String

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

public string ReverseString(string s) 
    {
        char[] arr = s.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }

public string ReverseString(string s) 
    {
        char[] arr = s.ToCharArray();
        int i=0,j = s.Length - 1;
        //交换值
        while(i<j)
        {
            arr[i] ^= arr[j];
            arr[j] ^= arr[i];
            arr[i] ^= arr[j];
            i++;
            j--;
        }
        return new string(arr);
    }


你可能感兴趣的:(LeetCode)