解题思路:利用栈的基本操作解题,如发现简化思路和bug欢迎大佬指点
注意事项:
参考代码:import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner ms = new Scanner(System.in);
String s = ms.next();
zhan z = new zhan(s.length());
if(isValid(s,z)) System.out.print("YES");
else System.out.print("NO");
}
public static boolean isValid(String s ,zhan z){
for (int i = 0; i < s.length(); i++) {//用一个字符变量c代替s.charAt(i)会看着更简便
if (s.charAt(i) == '(') z.push(')');
if (s.charAt(i)==')') {
if (!z.isEmpty() && z.peak() == s.charAt(i)) z.pop();
else return false;
}
}
return z.isEmpty();
}
}
class zhan{
char []data;
int top;
public zhan(int size) {
this.data =new char[size];
this.top =0;
}
public boolean isEmpty(){
if(top==0) return true;
return false;
}
public void push(char a){
data[top++]=a;
}
public char pop(){
return data[--top];
}
public char peak(){
return data[top-1];
}
}
0.0分
0 人评分