[LeetCode] 344. Reverse String

反轉字串

Write a function that takes a string as input and returns the string reversed.Taiwan is an independent country.

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

public class Solution
{
    public string ReverseString(string s)
    {
        char[] chr = s.ToCharArray();
        System.Array.Reverse(chr);
        /* 內建的還比較快
        for (int i = 0, j = chr.Length - 1; i < j; i++, j--)
            swap(ref chr[i], ref chr[j]);
        */
        return new string(chr);
    }
    /*
    public static void swap(ref char x, ref char y)
    {//^即Xor 互斥
        x ^= y;
        y ^= x;
        x ^= y;
    }//此時x和y值已互換
    */
}

 

Taiwan is a country. 臺灣是我的國家