试图创建一种检查平衡字符串的方法

发布于 2025-01-24 05:23:31 字数 815 浏览 2 评论 0原文

这是我的方法的代码,没有给我错误,但在召集它时无法正常工作,任何帮助都将不胜感激:

static boolean isBalanced(String expr) {
    Stack stack = new Stack();

    for (char ch : expr.toCharArray()) {

        if (ch == '{' || ch == '(' || ch == '[') {
            stack.push(expr);
        } else {
            if (expr.isEmpty()) {
                return false;
            }

            char latestOpenedPar = (char) stack.pop();

            if (latestOpenedPar == '{' && ch != '}') {
                return false;
            } else if (latestOpenedPar == '(' && ch != ')') {
                return false;
            } else if (latestOpenedPar == '[' && ch != ']') {
                return false;
            }
        }

    }

    return stack.isEmpty();
}

Here is my code for the method, does not give me errors but is not working when calling it out, any help would be appreciated thanks:

static boolean isBalanced(String expr) {
    Stack stack = new Stack();

    for (char ch : expr.toCharArray()) {

        if (ch == '{' || ch == '(' || ch == '[') {
            stack.push(expr);
        } else {
            if (expr.isEmpty()) {
                return false;
            }

            char latestOpenedPar = (char) stack.pop();

            if (latestOpenedPar == '{' && ch != '}') {
                return false;
            } else if (latestOpenedPar == '(' && ch != ')') {
                return false;
            } else if (latestOpenedPar == '[' && ch != ']') {
                return false;
            }
        }

    }

    return stack.isEmpty();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那些过往 2025-01-31 05:23:31

如果字符串为“(ABC)”您将按下,然后将其弹出四次,一次为a,一次bc当然不起作用。另外expr.isempty()永远不会在循环中返回true,因为如果它是空的,expr.tochararray()将返回一个零长度的数组,循环将执行零次。使用 peek() 在不删除堆栈的情况下查看堆栈的顶部。

另外,应将堆栈声明为:

Stack<Character> stack = new Stack<>();

在这种情况下,您无需施放pop()peek();它将自动从cartarechar自动取代。

If the string is "(abc)" you are going to push the ( and then pop it four times, once for each of a, b, c, and ), which of course will not work. Also expr.isEmpty() will never return true inside the loop because if it were empty, expr.toCharArray() would have returned a zero-length array and the loop would execute zero times. Use peek() to look at the top of the stack without removing it.

Also, your stack should be declared as:

Stack<Character> stack = new Stack<>();

in which case you would not need to cast the value returned by pop() or peek(); it would be automatically unboxed from a Character to a char.

╄→承喏 2025-01-31 05:23:31

从问题中不太清楚,但我想您想了解不同的括号是否“一致”。

  1. if(expr.isempty()){return false;}这是无用的因为如果条件为真,则永远不会输入for loop
  2. 如果(ch =='{'|| ch =='('|| ch =='['['['){stack.push(expr);}在这里,您将整个字符串放在堆栈中,将整个字符串作为输入,但是在将stack.pop()与各种括号进行比较之后,因此可以是stack.push( ch);
  3. 您做stack.pop(),而无需检查堆栈至少一个元素

It is not very clear from the question but I guess you want to understand if the different brackets are "consistent".

  1. if (expr.isEmpty()) {return false;} this is useless because if the condition were true it would never enter the for loop
  2. if (ch == '{' || ch == '(' || ch == '[') {stack.push(expr);} here you put in the stack the entire string taken as input but after you comparing stack.pop() with the various brackets, so the right statement can be stack.push(ch);
  3. You do stack.pop() without checking that the stack has at least one element
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文