1.2.7

1.2.7 #

解答 #

递归交换字符顺序,最后返回反序的字符串。

Mystery(ABCD)
  = Mystery(CD) + Mystery(AB)
    = Mystery(D) + Mystery(C) + Mystery(B) + Mystery(A)
      = DCBA

代码 #

Console.WriteLine(Mystery("Hello1"));

static string Mystery(string s)
{
    var n = s.Length;
    if (n <= 1)
        return s;
    var a = s.Substring(0, n / 2);
    var b = s.Substring(n / 2, n - n / 2);

    return Mystery(b) + Mystery(a);
}