英文:
trying to find a recursive method which returns a string between two parenthesis
问题
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) == ')' && str.charAt(0) == '(') {
return str;
}
return getParenthesis(str.substring(first, last));
}
英文:
only one pair of parenthsis are in the string and they are balanced, i cannot use methods which internally use for loops like contain, etc and regular expressions are prohibited.
Here is the code that i came up with but it always shows error.
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));
}*/
答案1
得分: 1
public static String getParenthesis(String str) {
int first = 0, size = str.length();
if (str.charAt(first) != '(')
return getParenthesis(str.substring(first + 1, size));
if (str.charAt(size - 1) != ')')
return getParenthesis(str.substring(first, size - 1));
return str.substring(first + 1, size - 1);
}
英文:
So, for example, given an input string:
Paren(thesis)String
you want to print:
thesis
Lets view this string as a character array and introduce two indices: first
and size
.
<!-- language: none -->
first size (== str.length())
| |_
str: P a r e n ( t h e s i s ) S t r i n g |_|
You want to increment first
until you reach the left brace - (
.
You want to decrement size
until you reach the right brace - )
.
The rest is just proper management of indices to satisfy String
's substring()
.
<!-- language: java -->
public static String getParenthesis(String str) {
int first = 0, size = str.length();
if (str.charAt(first) != '(')
return getParenthesis(str.substring(first + 1, size));
if (str.charAt(size - 1) != ')')
return getParenthesis(str.substring(first, size - 1));
return str.substring(first + 1, size - 1);
}
答案2
得分: 0
为了使递归函数正常工作,您需要使用额外的参数,但通常不希望在公共函数中处理这些参数,所以您可以使用另一个函数来解决这个问题。您的公共函数将不会是递归的,而您的私有函数将会是递归的。
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"));
}
}
正如您所见,我只是使用公共的 getParenthesis
函数来设置我的递归和私有函数 getParenthesisRec
。虽然您可以使用一个带有额外参数的单一函数,但那会变得混乱,因为您必须确保第一次调用时将正确的初始值传递给这些参数。在诸如Python之类的语言中,您可以为参数设置默认值,但在Java中不能这样做(尽管您不应该这样做,因为一旦在第一次调用中设置了不正确的值,就会出现问题)。
英文:
To make recursive functions works properly, you need to use extra parameters, but usually you don't want to handle with that in your public function, so you could fix that using another function. Your public function will not be recursive while your private function will be.
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"));
}
}
As you can see, I just use the public getParenthesis to setup my recursive and private function getParenthesisRec. Again, you could use one single function with extra parameters, but that would be a mess because you must ensure the first call you pass the correct first values to that parameters. This isn't necessary in languages like Python where you can set default values to your parameters, but in Java you can't (you shouldn't do it though, because again, you can mess it setting incorrect values in the first call).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论