【问题标题】:How do I determine if an expression has balanced brackets in a stack? [duplicate]如何确定表达式是否在堆栈中具有平衡括号? [复制]
【发布时间】:2015-05-21 20:08:18
【问题描述】:

所以我写了这段代码来确定一个表达式在堆栈中是否有平衡括号:

public static boolean isBalanced(String expr) {
    StringStack stack = new StringStackRefBased();
    try{
    for (int i = 0; i<expr.length(); i++){
        if (expr.charAt(i) == ('(')){
            stack.push("(");
        } else if (expr.charAt(i) == (')')){
            stack.pop();
        }
    }
    if (stack.isEmpty()){
        return true;
        } else {
            return false;
        }
    } catch (StringStackException e) {
        return false;
    }
}

问题是,即使表达式有平衡括号,堆栈也会继续返回 false,所以我的代码有什么问题?
这是 StringStackRefBased 的代码

public class StringStackRefBased implements StringStack {
    private StringNode head;

    public boolean isEmpty(){
        return head == null;
    }

    public void push(String item) throws StringStackException{
        head = new StringNode(item);
    }

    public String pop() throws StringStackException{
        String result = null;
        if(isEmpty()){
            throw new StringStackException("Empty Stack");
        }
        head.next = head;
        return head.toString();
    }

    public String peek() throws StringStackException{
        if (isEmpty()){
            throw new StringStackException("Stack underflow");
        }
        return head.toString();
    }
}

【问题讨论】:

  • 好吧,StringStack 里面没有太多东西,但是 StringStackRefBased 里面有所有的方法。我更新了代码以显示 StringStackRefBased。我最想知道的是,push 和 pop 方法是否正确?
  • 为什么不只使用一个 int 并执行 count++count--?然后最后你可以检查计数是否为零。
  • 问题在于你的StringStack,因为如果我用Java内置的Stack替换它,它就可以正常工作了。
  • 验证推送方式。我认为问题本身就存在。
  • 您对isEmpty() 的实现与您对push()pop() 的实现不兼容。在调用push() 之后,无论你调用了多少次pop,head 都不会是null

标签: java methods stack


【解决方案1】:

方法很好。如果我使用 Java 自己的堆栈:

class Main {

  public static boolean isBalanced(String expr) {
    Stack<String> stack = new Stack<>();
    try{
      for (int i = 0; i<expr.length(); i++){
        if (expr.charAt(i) == ('(')){
          stack.push("(");
        } else if (expr.charAt(i) == (')')){
          stack.pop();
        }
      }
      if (stack.isEmpty()){
        return true;
      } else {
        return false;
      }
    } catch (Exception e) {
      return false;
    }
  }

  public static void main(String[] args) {
    System.out.println(isBalanced("("));
    System.out.println(isBalanced("(()"));
    System.out.println(isBalanced("())"));
    System.out.println(isBalanced("((()))"));
    System.out.println(isBalanced("(()())"));
  }
}

将打印:

假
错误的
错误的
真的
真的

顺便说一句,您的 return 语句相当冗长,并且以这种方式使用异常是不好的做法。例外就是这样:一个例外(al case)。这比 IMO 更好:

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

  for (int i = 0; i < expr.length(); i++) {
    if (expr.charAt(i) == ('(')){
      stack.push("(");
    }
    else if (expr.charAt(i) == (')')) {
      if (stack.isEmpty()) {
        return false;
      }
      stack.pop();
    }
  }

  return stack.isEmpty();
}

这就是您的堆栈正常工作的方式:

class StringStack {

  private StringNode head = null;

  public boolean isEmpty(){
    return head == null;
  }

  public void push(String item) {
    StringNode oldHead = head;
    head = new StringNode(item);
    head.next = oldHead;
  }

  public String pop() throws StringStackException {
    if (isEmpty()) {
      throw new StringStackException("Empty Stack");
    }
    String result = head.item;
    head = head.next;
    return result;
  }

  public String peek() throws StringStackException {
    if (isEmpty()) {
      throw new StringStackException("Stack underflow");
    }
    return head.item;
  }

  static class StringNode {

    String item;
    StringNode next;

    public StringNode(String item) {
      this.item = item;
    }
  }
}

【讨论】:

  • @GriffeyDog,问题是关于他的方法(背后的逻辑),这很好。我只是向他展示了他需要查看他的筹码。如果他愿意提供他的堆栈的(完整)来源,我很乐意在我的回答中提供信息。
  • 是的,我知道。同样,如果他提供该课程的完整来源,我非常乐意用额外的信息编辑我的答案(如果有人在我之前没有这样做)。
  • 首先问题是我的推送和弹出方法无法正常工作,我不知道如何解决它们。对于推送,我正在尝试将旧头设置为前一个节点并继续增加堆栈的大小。
猜你喜欢
  • 2019-07-14
  • 2016-06-26
  • 2017-06-10
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 1970-01-01
相关资源
最近更新 更多