使用递归而不是栈来检查平衡的括号。

huangapple go评论65阅读模式
英文:

Check for balanced paranthesis using recursion without stack

问题

//Check for balanced parenthesis without using stack: DOUBT
static char findClosing(char c)
{
    if (c == '(')
        return ')';
    if (c == '{')
        return '}';
    if (c == '[')
        return ']';
    return Character.MIN_VALUE;
}
static boolean checkBracket(char[] arr, int n) {
    if (n == 0)
        return true;
    if (n == 1)
        return false;
    if (arr[0] == ')' || arr[0] == '}' || arr[0] == ']')
        return false;
    char closing = findClosing(arr[0]);
    int i, count = 0;
    for (i = 1; i < n; i++) {
        if (arr[i] == arr[0])
            count++;
        if (arr[i] == closing) {
            if (count == 0)
                break;
            count--;
        }
    }
    if (i == n)
        return false;
    if (i == 1)
        return checkBracket(Arrays.copyOfRange(arr, i + 1, n), n - 2);
    return checkBracket(Arrays.copyOfRange(arr, 1, i), i - 1) && checkBracket(Arrays.copyOfRange(arr, i + 1, n), n - i - 1);
}
英文:
//Check for balanced parenthesis without using stack:  DOUBT
static char findClosing(char c)
{
    if (c == &#39;(&#39;)
        return &#39;)&#39;;
    if (c == &#39;{&#39;)
        return &#39;}&#39;;
    if (c == &#39;[&#39;)
        return &#39;]&#39;;
    return Character.MIN_VALUE;
}
static boolean checkBracket(char [] arr,int n){
       if(n==0)
       return true ;
       if(n==1)
           return false;
       if(arr[0]==&#39;)&#39; || arr[0]==&#39;}&#39; || arr[0]==&#39;]&#39;)
           return false;
       char closing =findClosing(arr[0]);
    int i, count = 0;
    for (i = 1; i &lt; n; i++) {
        if (arr[i] == arr[0])
            count++;
        if (arr[i] == closing) {
            if (count == 0)
                break;
            count--;
        }
    }
    if (i == n)
        return false;
    if (i == 1)
        return checkBracket(Arrays.copyOfRange(arr, i + 1, n), n - 2);
    return checkBracket(Arrays.copyOfRange(arr, 1, i), i - 1) &amp;&amp; checkBracket(Arrays.copyOfRange(arr, (i + 1), n), n - i - 1);
}

I cannot understand the logic that what is done after initializing the variables count and I.

I know that a loop a written to traverse the array but I cannot get the logic that how it will find the closing brackets at appropriate positions.

Help.

答案1

得分: 1

以下是翻译好的内容:

Check for balanced paranthesis using recursion without stack

使用递归而不使用堆栈来检查括号的平衡。

The code does not use an explicit stack data structure, but it absolutely uses a stack: the call stack. Each method call involves a push onto that stack (or perhaps more than one, depending on how you characterize things), and each method return involves a corresponding pop (or pops).

该代码不使用显式的堆栈数据结构,但它绝对使用了一个堆栈:调用堆栈。每个方法调用都涉及将其推入堆栈(或者根据您如何描述事物,可能涉及多次推入),而每个方法返回都涉及相应的弹出。

if(n==0)
return true ;
if(n==1)
return false;
if(arr[0]=='(' || arr[0]=='{' || arr[0]==']')
return false;

如果字符(子)序列为空,那么它在平衡上是微不足道的。如果它只有一个元素,那么它在平衡上是微不足道的。如果第一个字符是考虑的闭合分隔符之一,那么它必然不平衡。这些是递归的基本情况。

char closing = findClosing(arr[0]);

确定与子序列的第一个字符匹配的闭合括号是什么。

int i, count = 0;
for (i = 1; i < n; i++) {
if (arr[i] == arr[0])
count++;
if (arr[i] == closing) {
if (count == 0)
break;
count--;
}
}

通过字符序列向前扫描以找到与序列的初始字符匹配的闭合分隔符。变量count用于处理当前正在处理的相同类型的括号对的嵌套。此时忽略其他类型的括号。当循环终止时,i是匹配的闭合括号的索引,或者它等于序列的长度n,这不是数组的有效索引。

if (i == n)
return false;

在这种情况下,没有找到匹配的闭合括号。

否则,

if (i == 1)
return checkBracket(Arrays.copyOfRange(arr, i + 1, n), n - 2);

如果匹配的闭合括号是紧随其后的字符,那么在它们之间没有其他括号。在这种情况下,仅当序列的尾部平衡时,整个序列才平衡。实际上,这是以下情况的特例,不真正需要它自己的情况。

否则,

return checkBracket(Arrays.copyOfRange(arr, 1, i), i - 1) && checkBracket(Arrays.copyOfRange(arr, (i + 1), n), n - i - 1);

整个序列仅在当前括号对之间的子序列平衡 序列的尾部平衡时才平衡。

英文:

> Check for balanced paranthesis using recursion without stack

The code does not use an explicit stack data structure, but it absolutely uses a stack: the call stack. Each method call involves a push onto that stack (or perhaps more than one, depending on how you characterize things), and each method return involves a corresponding pop (or pops).

> if(n==0)
> return true ;
> if(n==1)
> return false;
> if(arr[0]==')' || arr[0]=='}' || arr[0]==']')
> return false;

If the character (sub)sequence is empty then it is trivially balanced. If it has only one element then it is trivially unbalanced. If the first character is one of the closing delimiters considered then it is necessarily unbalanced. These are the base cases for the recursion.

> char closing =findClosing(arr[0]);

Determine which is the closing bracket matching the first character of the subsequence.

> int i, count = 0;
> for (i = 1; i < n; i++) {
> if (arr[i] == arr[0])
> count++;
> if (arr[i] == closing) {
> if (count == 0)
> break;
> count--;
> }
> }

Scan forward through the character sequence to find the closing delimiter matched to the initial character of the sequence. Variable count provides for handling nested bracket pairs of the same type as the one currently being handled. Brackets of other types are ignored at this time. When the loop terminates, i is the index of the matching close bracket, or it is equal to the length of the sequence, n, which is not a valid index into the array.

> if (i == n)
> return false;

In that case, no matching close bracket was found.

Otherwise,

> if (i == 1)
> return checkBracket(Arrays.copyOfRange(arr, i + 1, n), n - 2);

If the matching close bracket is the very next character then there are no others between. In that case, the overall sequence is balanced if and only if the tail of the sequence is balanced. This is actually a special case of the following, and doesn't really need its own case.

Otherwise,

> return checkBracket(Arrays.copyOfRange(arr, 1, i), i - 1) && checkBracket(Arrays.copyOfRange(arr, (i + 1), n), n - i - 1);

the overall sequence is balanced if and only if the subsequence between the current bracket pair is balanced and the tail of the sequence is balanced.

huangapple
  • 本文由 发表于 2023年3月31日 02:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定