有人可以解释这种反转字符串的递归 Java 方法吗?
Can someone explain this recursive Java method that reverses a string?
所以我是一名试图理解递归的大三学生。我理解一般概念,但不了解它是如何正确实施的。我发现这段代码非常简单,但似乎无法理解最后一行(递归)的工作原理。任何帮助和解释都会很棒,谢谢! (决赛马上就要到了,男孩我很紧张)
public static String reverse(String s) {
if (s.isEmpty())
return s;
return reverse(s.substring(1)) + s.charAt(0);
}
递归过程使用 substring 一个一个地删除 char ,因为 substring 取一个起始索引,在你的情况下是 1 所以如果我们的字符串是 "ABC" 那么 "ABC".substring(1) 那么 substring 将 return “卑诗省”
所以在你的情况下,反向是用子字符串处理
让我们的输入是反向方法的“abcd”
然后过程将喜欢用反向方法作为递归
bcd -> 1st reverse will call with this value
cd -> then reverse will call with cd
d -> then reverse will call with d
"" -> then reverse will call with "" as its blank string so reverse will terminate
一旦 reverse 方法到达 isEmpty 语句,s.charAt(0) 将开始,它将在 reverse 方法的 return 输出的末尾添加,因此它修改了输出,如
d
dc
dcb
dcba
所以整个过程会像:
input to reverse method :bcd
input to reverse method :cd
input to reverse method :d
input to reverse method : "" empty string
result received from reverse method
After modified result with return value from reverse method and charAt(0) operation : d
result received from reverse method d
After modified result with return value from reverse method and charAt(0) operation : dc
result received from reverse method dc
After modified result with return value from reverse method and charAt(0) operation : dcb
result received from reverse method dcb
After modified result with return value from reverse method and charAt(0) operation : dcba
为了清楚起见,这里是相同算法的迭代版本,它遍历遇到顺序中的字符串的字符,并将它们连接成[=17=中的新字符串]倒序,交换被加数:
public static String reverse(String str) {
String revStr = "";
for (char ch : str.toCharArray()) {
revStr = ch + revStr;
}
return revStr;
}
public static void main(String[] args) {
System.out.println(reverse("some text string"));
}
输出:
gnirts txet emos
所以我是一名试图理解递归的大三学生。我理解一般概念,但不了解它是如何正确实施的。我发现这段代码非常简单,但似乎无法理解最后一行(递归)的工作原理。任何帮助和解释都会很棒,谢谢! (决赛马上就要到了,男孩我很紧张)
public static String reverse(String s) {
if (s.isEmpty())
return s;
return reverse(s.substring(1)) + s.charAt(0);
}
递归过程使用 substring 一个一个地删除 char ,因为 substring 取一个起始索引,在你的情况下是 1 所以如果我们的字符串是 "ABC" 那么 "ABC".substring(1) 那么 substring 将 return “卑诗省” 所以在你的情况下,反向是用子字符串处理 让我们的输入是反向方法的“abcd”
然后过程将喜欢用反向方法作为递归
bcd -> 1st reverse will call with this value
cd -> then reverse will call with cd
d -> then reverse will call with d
"" -> then reverse will call with "" as its blank string so reverse will terminate
一旦 reverse 方法到达 isEmpty 语句,s.charAt(0) 将开始,它将在 reverse 方法的 return 输出的末尾添加,因此它修改了输出,如
d
dc
dcb
dcba
所以整个过程会像:
input to reverse method :bcd
input to reverse method :cd
input to reverse method :d
input to reverse method : "" empty string
result received from reverse method
After modified result with return value from reverse method and charAt(0) operation : d
result received from reverse method d
After modified result with return value from reverse method and charAt(0) operation : dc
result received from reverse method dc
After modified result with return value from reverse method and charAt(0) operation : dcb
result received from reverse method dcb
After modified result with return value from reverse method and charAt(0) operation : dcba
为了清楚起见,这里是相同算法的迭代版本,它遍历遇到顺序中的字符串的字符,并将它们连接成[=17=中的新字符串]倒序,交换被加数:
public static String reverse(String str) {
String revStr = "";
for (char ch : str.toCharArray()) {
revStr = ch + revStr;
}
return revStr;
}
public static void main(String[] args) {
System.out.println(reverse("some text string"));
}
输出:
gnirts txet emos