使用堆栈检查括号/括号
Posted
技术标签:
【中文标题】使用堆栈检查括号/括号【英文标题】:Parenthesis/Bracket checking using stack 【发布时间】:2022-01-02 05:53:49 【问题描述】:我正在使用字符串检查字符串中的平衡括号。我已经使用类制作了实现堆栈。首先,当我使用 util 包运行该程序时,它给出了正确的答案,但是当我制作自定义堆栈时,这给出了错误的输出。我在这方面做错了什么..
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[is]valid)
(the[is]valid))
the(is[valid])
(this](isvalid)
样本输出:
是的
错误
是的
错误
【问题讨论】:
“为什么我的代码不起作用?”这不是我们想要的问题。你可以说得更详细点吗?你试过调试吗? 【参考方案1】:错误
-
您没有使用静态函数或全局变量
您必须在每次新字符串检查后将 top 重置为 -1
在执行 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");
【讨论】:
以上是关于使用堆栈检查括号/括号的主要内容,如果未能解决你的问题,请参考以下文章