簡體   English   中英

使用堆棧匹配括號

[英]Matching Parantheses using stack

下面的代碼是檢查給定的字符串是否具有平衡括號或不使用堆棧。 輸入“[]”時出錯 output。 它應該打印正確,但我得到的結果是錯誤的。

import java.util.*;
class Solution{
    public static void main(String[] argh) {
        Scanner sc = new Scanner(System.in);
        Stack st = new Stack();
        boolean flag = true;
        while (sc.hasNext()) {
            String input = sc.next();
            //Complete the code
            for (int i = 0; i < input.length(); i++) {
                flag = false;
                if ((input.charAt(i) == '{') || (input.charAt(i) == '(') || (input.charAt(i) == '[')) {
                    st.push(input.charAt(i));
                    continue;
                }
                if ((input.charAt(i) == '}') || (input.charAt(i) == ')') || (input.charAt(i) == ']')) {
                    if (st.isEmpty()) {
                        flag = false;
                    } else {
                        char item = (char) st.pop();
                        if ((item == '(') && (input.charAt(i) == ')'))
                            flag = true;
                        if ((item == '{') && (input.charAt(i) == '}'))
                            flag = true;
                        if ((item == '[') && (input.charAt(i) == ']'))
                            flag = true;
                    }
                }
            }
            if (!st.isEmpty())
                flag = false;
            System.out.println(flag);
        }
    }
}

我已經在下面復制了您的代碼,並進行了一些更改(由注釋標識),這些更改應該可以使其正常工作。

import java.util.*;
class Solution{
    public static void main(String[] argh) {
        Scanner sc = new Scanner(System.in);
        Stack st = new Stack();
        while (sc.hasNext()) {
            // jim - Moved this to inside the loop.
            // Assume the input is good unless proven otherwise.
            boolean flag = true;
            String input = sc.next();
            //Complete the code
            // jim - Added the "&& flag == true" to exit if the input is bad
            for (int i = 0; i < input.length() && flag == true; i++) {
                // jim - Commented this out. We'll assume it's good. If the input
                // is found to be bad, we set the flag, and the
                // "&& flag == true" condition above will exit the loop.
                // flag = false; 
                if ((input.charAt(i) == '{') || (input.charAt(i) == '(') || (input.charAt(i) == '[')) {
                    st.push(input.charAt(i));
                    continue;
                }
                if ((input.charAt(i) == '}') || (input.charAt(i) == ')') || (input.charAt(i) == ']')) {
                    if (st.isEmpty()) {
                        flag = false;
                    } else {
                        char item = (char) st.pop();
                        // jim - here, check for not equal, and set to false.
                        if ((item == '(') && (input.charAt(i) != ')'))
                            flag = false;
                        if ((item == '{') && (input.charAt(i) != '}'))
                            flag = false;
                        if ((item == '[') && (input.charAt(i) != ']'))
                            flag = false;
                    }
                }
                else {
                    // jim - added this 'else' clause.
                    // If there's any character other than [{()}], then it's
                    // bad input.
                    flag = false;
                }
            }
            if (!st.isEmpty())
                flag = false;
            System.out.println(flag);
        }
    }
}

您可以對代碼進行一些改進,但我記錄的這些更改應該可以使其正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM