解题思路:利用栈的基本操作解题,如发现简化思路和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 人评分
【偶数求和】 (C语言代码)浏览:646 |
WU-蓝桥杯算法提高VIP-勾股数 (C++代码)浏览:1605 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:1428 |
罗列完美数 (C语言代码)浏览:491 |
剪刀石头布 (C++代码)浏览:1711 |
数组输出 (C语言代码)浏览:703 |
简单的a+b (C语言代码)浏览:630 |
简单的a+b (C语言代码)浏览:460 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:489 |
盐水的故事 (C语言代码)浏览:1528 |