Java使用递归反转字符串
示例
下面是递归代码以反转字符串
/**
* Just a snippet to explain the idea of recursion
*
**/
public class Reverse {
public static void main (String args[]) {
String string = "hello world";
System.out.println(reverse(string)); //打印dlrowolleh
}
public static String reverse(String s) {
if (s.length() == 1) {
return s;
}
return reverse(s.substring(1)) + s.charAt(0);
}
}
热门推荐
10 中秋祝福语简短版大全
11 简短祝福语中考女孩的话
12 澳门姐妹生日祝福语简短
13 入学校简短祝福语
14 表达家人的祝福语简短
15 新年留言老师祝福语简短
16 妈妈生日贺卡祝福语 简短
17 六一加端午祝福语简短
18 元旦在岗祝福语简短精辟