递归函数中的奇怪/奇怪行为

时间:2014-06-06 17:24:43

标签: java string recursion

我正在尝试编写一个函数来检查字符串是否是前一个字符串上的一组操作的结果。具体来说,一个字符串&#39; b&#39;是一个字符串&#39; a&#39;如果它遵循相同的字符顺序,只有相同的字符,但可能会有它们相乘。例如: "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); }

代码如下:

return (i+1)

该计划没有按预期运作。 现在这里有一个奇怪的事情:通过调试器运行它,它按预期执行所有操作,但是,当它到达&#34; i&#34;命令,而不是实际返回它,它开始执行递归调用由于某种原因,同时减少isTrans(s,i,t,j)的值,直到它达到0,然后只返回它,导致错误否定。更具体地说,它上升到堆栈并执行&#39; i的递归调用。

我想知道为什么这样做,甚至不仅仅是一种解决此类问题的方法。它甚至不会通过iff进入,但会立即进入递归调用,将"aabba"的值一直减少到0,然后才返回它。

感谢任何评论!

根据调试器,

编辑以明确它的作用。如果我尝试查看"aba"i的变换(它在上面的定义下),则程序会达到2 - return (i+1)的所需值。然而,它然后到达命令i,然后突然返回给定代码中的第17行,然后是代码中的下一个递归调用,然后返回到它 - 同时减少0的值回到i。只有这样它才会在函数外部执行命令。

编辑2:经过一些调整并使用代码后,似乎没有连接到返回语句,最后是函数&#39;跳&#39;向后,跳过“如果”到递归调用 - 不会执行它们,但会将0缩减为{{1}},然后才会继续。是否与我调用isTrans(s, 0 ,t,1)有关?

1 个答案:

答案 0 :(得分:1)

您的退出条件为return i+1。这将永远不会工作,即使你的逻辑确实如此,因为你返回的结果总是比String的长度多一个。

以下是代码中可能出现的情况:

  1. 首先如果满足条件
  2. j逐渐增加。
  3. 递归电话。
  4. 如果满足条件,则不再需要,因为i和j不再指向同一个字母。
  5. 返回i + 1
  6. 要以递归方式执行此操作,您可能会执行以下操作:

    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));
        }
    
    }
    

    希望你发现它有用。

相关问题