Algs4-1.2.7以下递归函数的返回值是什么?
1.2.7以下递归函数的返回值是什么?
public static String mystery(String s)
{
int N=s.length();
if (N<=1) return s;
String a=s.substring(0,N/2);
String b=s.substring(N/2,N);
return mystery(b)+mystery(a);
}
解:将mystery的参数s反序。
public class Test
{
public static void main(String[] args)
{
String s1="0123456789";
String s2="123456789";
StdOut.printf("s1=%s,mystery(s1)=%s\n",s1,mystery(s1));
StdOut.printf("s2=%s,mystery(s2)=%s\n",s2,mystery(s2));
}//end main
public static String mystery(String s)
{
int N=s.length();
if (N<=1) return s;
String a=s.substring(0,N/2);
String b=s.substring(N/2,N);
return mystery(b)+mystery(a);
}
}//end class Test
public static String mystery(String s)
{
int N=s.length();
if (N<=1) return s;
String a=s.substring(0,N/2);
String b=s.substring(N/2,N);
return mystery(b)+mystery(a);
}
解:将mystery的参数s反序。
public class Test
{
public static void main(String[] args)
{
String s1="0123456789";
String s2="123456789";
StdOut.printf("s1=%s,mystery(s1)=%s\n",s1,mystery(s1));
StdOut.printf("s2=%s,mystery(s2)=%s\n",s2,mystery(s2));
}//end main
public static String mystery(String s)
{
int N=s.length();
if (N<=1) return s;
String a=s.substring(0,N/2);
String b=s.substring(N/2,N);
return mystery(b)+mystery(a);
}
}//end class Test