-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathExp_isValid.java
More file actions
32 lines (27 loc) · 850 Bytes
/
Exp_isValid.java
File metadata and controls
32 lines (27 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package stackqueue.test;
import stackqueue.SeqStack;
/**
* 利用栈判断左右括号是否匹配
*/
public class Exp_isValid {
public static String isValid(String expstr) {
SeqStack<String> stack = new SeqStack<String>(expstr.length());// 顺序栈
for (int i = 0; i < expstr.length(); i++) {
char ch = expstr.charAt(i);
switch (ch) {
case '(':
stack.push(ch + "");// 左括号入栈
break;
case ')':
// 遇见右括号时,出栈
if (stack.isEmpty() || !stack.pop().equals("("))
return "期望(";// 判断出战字符是否为左括号
}
}
return (stack.isEmpty()) ? "" : "期望)";// 返回空串表示没有错误
}
public static void main(String[] args) {
String expstr = "((1+2)*3+4))(";
System.out.println(expstr + " " + isValid(expstr));
}
}