我的任務是遞回和迭代地搜索給定輸入中的字串。更具體地說,我需要確保所有括號括號都有一個相反的括號來匹配它。如果括號都匹配,我的代碼回傳 true,否則回傳 false。我的問題在于遞回,因為我正在努力跟蹤括號。我下面的代碼在呼叫一次時運行良好,但如果我多次呼叫它,整數會交叉跟蹤,它會導致超出范圍的例外。我想知道在呼叫函式時存盤值的最佳方式,以及在再次呼叫函式時如何重置整數。
換句話說,如何在呼叫代碼后重置這些數字,以便在再次呼叫時作業,以及在遞回中跟蹤數字的最佳方法是什么?
public static int tempSpot = 0; // this integer stores the position of the string im at
// since I'm not allowed to use a for loop to serve the same purpose
public static int openCount1 = 0; // track the count of "("
public static int closeCount1 = 0; // track the count of ")"
public static boolean balanceParenthesesRecursive(String str){
if (str.charAt(tempSpot) == '(') {
openCount1 ;
}
if (str.charAt(tempSpot) == ')') {
closeCount1 ;
}
tempSpot ;
if (tempSpot == str.length()) { // prevents recursion if next call will be out of bounds
return openCount1 == closeCount1;
} else {
balanceParenthesesRecursive(str);
}
return openCount1 == closeCount1;
}
uj5u.com熱心網友回復:
我相信,您不需要“存盤”任何資料。您可以將資料作為引數傳遞給下一個遞回回圈。像這樣的東西:
public static boolean balanceParenthesesRecursive(
String str, int tempSpot, int openCount, int closeCount
) {
if (str.charAt(tempSpot) == '(') {
openCount ;
}
if (str.charAt(tempSpot) == ')') {
closeCount ;
}
tempSpot ;
if (tempSpot == str.length()) {
return openCount == closeCount;
} else {
return balanceParenthesesRecursive(str, tempSpot, openCount, closeCount);
}
}
public static void main(String[] args) {
System.out.println("Positive result = " balanceParenthesesRecursive("this is (positive) test string.", 0, 0, 0));
System.out.println("Negative result = " balanceParenthesesRecursive("this is (negative)) test string.", 0, 0, 0));
}
更新:您說您要求該方法應僅接受一個引數(字串)。有另一種選擇,但它真的很丑。完成作業后清除靜態變數:
public static int tempSpot = 0; // this integer stores the position of the string im at
// since I'm not allowed to use a for loop to serve the same purpose
public static int openCount1 = 0;
public static int closeCount1 = 0; // track the count of "(" or ")"
public static boolean balanceParenthesesRecursive(String str){
if (str.charAt(tempSpot) == '('){
openCount1 ;
}
if (str.charAt(tempSpot) == ')'){
closeCount1 ;
}
tempSpot ;
if (tempSpot == str.length()){ // prevents recursion if next call will be out of bounds
boolean result = openCount1 == closeCount1;
tempSpot = 0;
openCount1 = 0;
closeCount1 = 0;
return result;
} else {
return balanceParenthesesRecursive(str);
}
}
public static void main(String[] args) {
System.out.println("Positive result = " balanceParenthesesRecursive("this is (positive) test string."));
System.out.println("Negative result = " balanceParenthesesRecursive("this is (negative)) test string."));
}
備選方案#2。也許可以將遞回方法“包裝”成非遞回:
public static boolean balanceParenthesesRecursive(
String str, int tempSpot, int openCount, int closeCount
) {
if (str.charAt(tempSpot) == '(') {
openCount ;
}
if (str.charAt(tempSpot) == ')') {
closeCount ;
}
tempSpot ;
if (tempSpot == str.length()) {
return openCount == closeCount;
} else {
return balanceParenthesesRecursive(str, tempSpot, openCount, closeCount);
}
}
public static boolean balanceParentheses(String str) {
return balanceParenthesesRecursive(str, 0, 0, 0);
}
public static void main(String[] args) {
System.out.println("Positive result = " balanceParentheses("this is (positive) test string."));
System.out.println("Negative result = " balanceParentheses("this is (negative)) test string."));
}
uj5u.com熱心網友回復:
現有答案很好,但為了解決您的評論,以下是通過使用實體類/物件而不使用靜態變數的解決方案。
首先,我們創建一個新類,對于這個例子,我使用SomeClass如下,并注意沒有什么是靜態的:
public class SomeClass {
//Variables will be reset to the below values every time you create a new instance of this object
private int tempSpot = 0;
private int openCount1 = 0;
private int closeCount1 = 0;
public boolean balanceParenthesesRecursive(String str) {
if (str.charAt(tempSpot) == '(') {
openCount1 ;
}
if (str.charAt(tempSpot) == ')') {
closeCount1 ;
}
tempSpot ;
if (tempSpot == str.length()) { // prevents recursion if next call will be out of bounds
return openCount1 == closeCount1;
} else {
balanceParenthesesRecursive(str);
}
return openCount1 == closeCount1;
}
}
然后呼叫這個類并使用我們首先需要使用的方法new SomeClass()來創建該類的實體,然后我們可以呼叫該方法并得到這樣的結果:
Boolean result = new SomeClass().balanceParenthesesRecursive(yourString);
旁注,一個更好的解決方案可能是合并我的兩個建議以同時擁有一個啟動方法并仍然將方法實體化到類中(不是靜態的)。這是一個更安全的選擇,但在大多數情況下不是必需的,也不需要是非靜態的。注意 starter 方法是公共的,而帶有變數的遞回方法是私有的,因此不能從類/物件外部訪問它:
public class SomeClass {
//public starter method that starts the values at 0
public boolean balanceParenthesesRecursive(String str) {
return balanceParenthesesRecursive(str, 0, 0, 0);
}
//tempSpot stores the position of the string im at
//openCount1 tracks the count of "("
//closeCount1 tracks the count of ")"
private boolean balanceParenthesesRecursive(String str, int tempSpot, int openCount1, int closeCount1) {
if (str.charAt(tempSpot) == '(') {
openCount1 ;
}
if (str.charAt(tempSpot) == ')') {
closeCount1 ;
}
tempSpot ;
if (tempSpot == str.length()) { // prevents recursion if next call will be out of bounds
return openCount1 == closeCount1;
} else {
balanceParenthesesRecursive(str);
}
return openCount1 == closeCount1;
}
}
您將使用新的類實體和方法呼叫從第二個選項中獲取結果:
Boolean result = new SomeClass().balanceParenthesesRecursive(yourString);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460995.html
上一篇:解決此遞回的最佳方法是什么?
下一篇:為什么高階函式只呼叫一次
