Algs4-1.2.7以下遞歸函數的返回值是什麽?
阿新 • • 發佈:2018-10-25
mys style 技術分享 分享圖片 webp data log -i 參數
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
Algs4-1.2.7以下遞歸函數的返回值是什麽?