簡體   English   中英

使用堆棧檢查括號/括號

[英]Parenthesis/Bracket checking using stack

我正在使用字符串檢查字符串中的平衡括號。 我已經使用 class 制作了工具堆棧。 首先,當我使用 util package 運行該程序時,它給出了正確的答案,但是當我制作自定義堆棧時,這給出了錯誤的輸出。我在這方面做錯了什么..

import java.util.Scanner;

public class Stack {
    int top;
    char []a=new char[10];
    
    public void push(char c)
     {
        if(top <a.length-1)
         {
            top++;
             a[top]=c;
         }
     }
    
    public char pop()
     {
         if(top > 0)
         {
             top--;
             char c=a[top];
             return c;
         }
        return 0;
         
     }
    
    public boolean isEmpty()
     {
        return (top==-1);
     }
    
    public char peek()
     {
        return a[top];
     }
    
    void displayStack()
    {
        for(int i=0;i<=top;i++)
          System.out.print(a[i]+" ");
    }
    public static boolean CheckParentesis(String str)
    {
        if (str.isEmpty())
            return true;

        Stack stack = new Stack();
        for (int i = 0; i < str.length(); i++)
        {
            char Symbol = str.charAt(i);
            if (Symbol == '{' || Symbol == '(' || Symbol == '[')
            {
                stack.push(Symbol);
                continue;
            }
            
            if (Symbol == '}' || Symbol == ')' || Symbol == ']')
            {
                if (stack.isEmpty())
                    return false;

                char last = stack.peek();     //peek checks top element of stack without removing it...
                if (Symbol == '}' && last == '{' || Symbol == ')' && last == '(' || Symbol == ']' && last == '[')
                    stack.pop();
                     
                else 
                    return false;
            }
        
        }
        return stack.isEmpty();
    }
    
    public static void main(String[] args) {
        {  
            Scanner sc = new Scanner(System.in);          
            String[] str = new String [sc.nextInt()];      
            //consuming the <enter> from input above  
            sc.nextLine();   

            for (int i = 0; i < str.length; i++)   
            {  
              str[i] = sc.nextLine();  
            } 
            for(String s: str)   
            {  
                if(CheckParentesis(s)==true)
                       System.out.println("TRUE"); 
                   else
                      System.out.println("FALSE");
            }  
        }  
        
    }
}

樣本輸入:

4

([是]{有效})

([是]{有效的))

{the(是[有效])}

(這個](是}{有效的)

樣品 Output:

真的

錯誤的

真的

錯誤的

錯誤

  1. 您沒有使用 static function 或全局變量
  2. 每次新字符串檢查后,您必須將頂部重置為 -1
  3. 在執行 pop() 時,您應該首先捕獲元素,然后減少堆棧索引值
import java.util.Scanner;

public class Stack {
    static int top = -1;
    static char[] a = new char[10];

    public static void clear() {
        top = -1;
    }

    public static void push(char c) {
        if (top < a.length - 1) {
            top++;
            a[top] = c;
        }
    }

    public static char pop() {
        if (top >= 0) {

            char c = a[top];
            top--;
            return c;
        }
        return 0;

    }

    public static boolean isEmpty() {
        return (top == -1);
    }

    public static char peek() {
        return a[top];
    }

    static void displayStack() {
        for (int i = 0; i <= top; i++)
            System.out.print(a[i] + " ");
        System.out.println();
    }

    public static boolean CheckParentesis(String str) {
        if (str.isEmpty())
            return true;

        Stack stack = new Stack();
        stack.clear();
        for (int i = 0; i < str.length(); i++) {
            // stack.displayStack();
            char Symbol = str.charAt(i);
            if (Symbol == '{' || Symbol == '(' || Symbol == '[') {
                stack.push(Symbol);
                continue;
            }

            if (Symbol == '}' || Symbol == ')' || Symbol == ']') {
                if (stack.isEmpty())
                    return false;

                char last = stack.peek(); //peek checks top element of stack without removing it...
                if (
                    (Symbol == '}' && last == '{') ||
                    (Symbol == ')' && last == '(') ||
                    (Symbol == ']' && last == '[')
                )
                    stack.pop();

                else
                    return false;
            }

        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        {
            Scanner sc = new Scanner(System.in);
            String[] str = new String[sc.nextInt()];
            //consuming the <enter> from input above  
            sc.nextLine();

            for (int i = 0; i < str.length; i++) {
                str[i] = sc.nextLine();
            }
            for (String s: str) {
                if (CheckParentesis(s) == true)
                    System.out.println("TRUE");
                else
                    System.out.println("FALSE");
            }
        }

    }
}

暫無
暫無

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

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