Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图找到一个递归方法,该方法返回两个括号之间的字符串_Java_Recursion - Fatal编程技术网

Java 试图找到一个递归方法,该方法返回两个括号之间的字符串

Java 试图找到一个递归方法,该方法返回两个括号之间的字符串,java,recursion,Java,Recursion,字符串中只有一对parenths,它们是平衡的,我不能使用内部用于contain等循环的方法,并且禁止使用正则表达式 这是我提出的代码,但它总是显示错误 public static String getParenthesis(String str) { int first = 1 , last = str.length()-2; if(str.charAt(0) =='(') { first = 0; }

字符串中只有一对parenths,它们是平衡的,我不能使用内部用于contain等循环的方法,并且禁止使用正则表达式

这是我提出的代码,但它总是显示错误

    public static String getParenthesis(String str) {
    int first = 1 , last = str.length()-2;
        if(str.charAt(0) =='(')
        {
            first = 0;
        }
            
        if (str.charAt(str.length()-1) == ')')
            last++;
        if(str.charAt(str.length()-1) == ')'&& str.charAt(0)=='(')
        return str;
        
        return getParenthesis(str.substring(first, last));

}*/

要使递归函数正常工作,您需要使用额外的参数,但通常不希望在公共函数中处理这些参数,因此可以使用其他函数修复这些参数。您的公共函数将不是递归函数,而您的私有函数将是递归函数

public class HelloWorld {

    private static String getParenthesisRec(String str, String res, boolean parenthesisFound) {
        if (str.length() == 0) {
            // Just in case...
            return "";
        }

        char currentChar = str.charAt(0);

        if (parenthesisFound && currentChar == ')') {
            // Gotcha!
            return res;
        } else if (parenthesisFound && currentChar != ')') {
            res += currentChar;
        }

        String substring = str.substring(1, str.length());

        if (currentChar == '(') {
            return HelloWorld.getParenthesisRec(substring, "", true);
        }

        return HelloWorld.getParenthesisRec(substring, res, parenthesisFound);
    }

    public static String getParenthesis(String str) {
        return HelloWorld.getParenthesisRec(str, "", false);
    }

    public static void main(String []args) {
        System.out.println(HelloWorld.getParenthesis("Example t(o StackOver)flow"));
    }
}

如您所见,我只是使用公共get括号来设置递归函数和私有函数get括号isrec。同样,您可以使用带有额外参数的单个函数,但这将是一个混乱,因为您必须确保在第一次调用时将正确的第一个值传递给该参数。在Python这样的语言中,这是不必要的,因为您可以为参数设置默认值,但在Java中,这是不必要的(不过,您不应该这样做,因为在第一次调用中,您可能会设置不正确的值)。

因此,例如,给定一个输入字符串:

Paren(thesis)String
您要打印:

thesis

让我们将此字符串视为字符数组,并引入两个索引:
first
size

first size(=str.length())
|                                     |_
str:P a r e n(th he s i s)s t r i n g|_|
您希望先递增
,直到到达左大括号-

您希望减小
大小
,直到到达右大括号-

剩下的就是对索引的适当管理,以满足
字符串的要求

publicstaticstringget圆括号(stringstr){
int first=0,size=str.length();
如果(str.charAt(first)!='(')
返回get圆括号(str.substring(first+1,size));
如果(str.charAt(大小-1)!=')')
返回get圆括号(str.substring(第一个,大小为-1));
返回str.substring(第一个+1,大小-1);
}

请提供错误消息。一对括号是平衡的是什么意思?