Java 递归函数中的奇/奇行为
我试图编写一个函数,检查一个字符串是否是前一个字符串上的一组操作的结果。具体来说,字符串“b”是字符串“a”的转换,如果它遵循相同的字符顺序,只有相同的字符,但可能会将它们相乘。例如:Java 递归函数中的奇/奇行为,java,string,recursion,Java,String,Recursion,我试图编写一个函数,检查一个字符串是否是前一个字符串上的一组操作的结果。具体来说,字符串“b”是字符串“a”的转换,如果它遵循相同的字符顺序,只有相同的字符,但可能会将它们相乘。例如: “aabba”是对“aba”的转换,但不是对“abaa”的转换,因为它的末尾有两个'a' 代码如下: public static boolean isTrans(String s, String t) { if(s.equals(t)){return true;} //obviously the latt
“aabba”
是对“aba”
的转换,但不是对“abaa”
的转换,因为它的末尾有两个'a'
代码如下:
public static boolean isTrans(String s, String t)
{
if(s.equals(t)){return true;} //obviously the latter is transformation
else if(s.length()==0||t.length()==0){return false;} //either is empty, can't be transformation
else if(s.charAt(0)!=t.charAt(0)){return false;} //obviously not transformation
else {return (s.length()==(isTrans(s,0,t,1)));} //recursive method below, note s.charAt(0)==t.charAt(0).
}
private static int isTrans(String s, int i, String t, int j)
{
if(i < s.length() && j < t.length()) //only as long as the indexes are within right bound
{
if(s.charAt(i) == t.charAt(j)) //character at 'transformed' string is the character at original string
{
if((j+1)<t.length()) //only as long as there are more characters
{
j++;
isTrans(s,i,t,j); //check next character at transformed string
}
}
else if((i+1)<s.length()) //ensures there is a next character at string s
{
if(s.charAt(i+1) == t.charAt(j)) //character is the next chracter at original string
{
i++;
if((j+1)<t.length()) //only as long as there are more characters
{
j++;
isTrans(s,i,t,j); //checks next characters at strings
}
}
else{i=-1;} //not transformation
}
else{i=-1;} //not transformation
}
return (i+1);
}
公共静态布尔isTrans(字符串s、字符串t)
{
如果(s.equals(t)){return true;}//显然后者是变换
否则,如果(s.length()==0 | | t.length()==0){return false;}//其中一个为空,则无法转换
else如果(s.charAt(0)!=t.charAt(0)){return false;}//显然不是转换
else{return(s.length()==(isTrans(s,0,t,1));}//下面的递归方法,注意s.charAt(0)=t.charAt(0)。
}
私有静态int isTrans(字符串s、int i、字符串t、int j)
{
if(i 如果((j+1)您的退出条件是返回i+1
。这永远不会起作用,即使您的逻辑起作用,因为您返回的结果总是比字符串长度多一个
下面是代码中可能发生的情况:
首先,如果条件满足
j是递增的
递归调用
如果条件满足,则不再需要,因为i和j不再指向同一个字母
它返回i+1
要递归地执行此操作,您可能会执行以下操作:
public class Main {
public static void main(String args[])
{
String s = "aba";
String t = "abz";
System.out.println(isTrans(0, 0, s, t));
}
public static boolean isTrans(int si, int ti,String s ,String t)
{
// we have run out of source characters, or the source character has passed the transformation character. This is not a transformation.
if (si > ti || si == s.length())
{
return false;
}
// we are at the end of the transformation string. This might be a transformation.
if(ti == t.length())
{
return si == s.length()-1; // If we're at the end of our source string, otherwise it isn't.
}
// if the current positions contain identical characters...
if (checkChar(si, ti, s, t))
{
// advance the transformation index.
ti++;
}
else
{
// otherwise, advance the source index.
si++;
}
// recursion.
return isTrans(si, ti, s, t);
}
private static boolean checkChar(int si, int ti, String s, String t)
{
return (s.charAt(si) == t.charAt(ti));
}
}
希望对您有所帮助。您确定它是向下而不是向上的吗?换句话说,您确定您使用的函数不是递归调用,返回语句将您带回来了吗?我不知道字符串转换是什么。您能给出一些字符串的解释示例,这些字符串是/不是ea的转换吗ch other(为什么)?您还使用==
比较字符串,您很想将其更改为。equals()
,请注意,递归调用时没有使用isTrans()
的返回值,这会导致问题吗?第一次调用将始终返回[-1,1]中的内容.String equality不是用s==t
测试的。如果你注释掉这些调用,它的功能将完全相同,除了它们可能引发的任何异常。如果你看,它们没有改变任何东西。很抱歉,我不清楚什么是转换,用它更新了原始问题。“aba”是“aba”的转换根据我的定义,“aaba”,“aabbbbbba”也是等等。退出条件是返回i+1
,而不是-1。@Studentmath我已经更改了我的答案,以更好地反映您更新的问题。我希望这会有所帮助。我想我现在明白我做错了什么,我想我明白了如何实现您的想法来解决我的问题。非常感谢,它确实澄清了我的问题!