-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidParenthese.java
More file actions
78 lines (74 loc) · 1.96 KB
/
Copy pathValidParenthese.java
File metadata and controls
78 lines (74 loc) · 1.96 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package EasyCode;
import java.util.Stack;
public class ValidParenthese {
public static void main(String[] args) {
Stack<Character> stack = new Stack<>();
String s = "(([]{}))";
for(int i = 0 ; i < s.length() ; i++){
char ch = s.charAt(i);
if(ch == '(' || ch == '[' || ch =='{')
stack.push(ch);
else{
if(stack.isEmpty())
System.out.println("false");
char topstack = stack.pop();
if(ch == ')' && topstack != '(') System.out.println("false");
else if(ch == ']' && topstack != '[') System.out.println("false");
else if(ch == '}' && topstack != '{') System.out.println("false");
}
}
System.out.println(stack.isEmpty());
}
// if(s.isEmpty() || s.length() == 0) System.out.println("True");
// ArrayStack stack = new ArrayStack(20);
// stack.push(s.charAt(0));
// System.out.println(isValid(s));
// }
// public static boolean isValid(String s) {
// if(s == "" || s.length() == 0)
// return true;
//
// Stack<Character> stack = new Stack<Character>();
// stack.push(s.charAt(0));
//
// for(int i=1; i<s.length(); i++){
// if(!stack.isEmpty()){
// if(stack.peek().equals((char)(s.charAt(i)-1)) || stack.peek().equals((char)(s.charAt(i)-2))){
// stack.pop();
// }else {
// stack.push(s.charAt(i));
// }
// }else {
// stack.push(s.charAt(i));
// }
// }
// if(stack.isEmpty())
// return true;
// return false;
}
//写一个基于zifu的顺序栈
class ArrayStack{
private char [] tmps;
private int count;
private int n;
//构造函数
public ArrayStack(int n){
this.tmps = new char [n];
this.n = n;
this.count = 0;
}
//入栈操作
public boolean push(char c){
if(count == n) return false;
tmps[count] = c;
++count;
return true;
}
//出栈操作
public char pop(){
if(count == 0) return ' ';
char c = tmps[count - 1];
--count;
return c;
}
}