-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJudgeChar.java
More file actions
39 lines (33 loc) · 821 Bytes
/
Copy pathJudgeChar.java
File metadata and controls
39 lines (33 loc) · 821 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
33
34
35
36
37
38
39
package com.wgcris.LeetcodeAlgorithm;
import java.util.Stack;
/*
* 括号匹配
*/
public class JudgeChar {
public static void judge(String str){
Stack<Character>stack= new Stack<Character>();
for(int i=0;i<str.length();i++){
char c1= str.charAt(i);
//先将遇到的符号压栈
if(c1=='('||c1=='{'||c1=='[')
stack.push(c1);
else if(stack.peek()=='{'&& str.charAt(i)=='}'||stack.peek() == '(' && str.charAt(i) == ')'||stack.peek() == '[' && str.charAt(i) == ']')
stack.pop();
}
if(stack.isEmpty()){
System.out.println("yes");
}
else{
System.out.println("no");
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println(match("([aghfdlfdfd])"));
String str="[(a{dafad}b)]";
judge(str);
}
}