public class MyStack {
private int[] array;
private int maxSize;
private int top;
public MyStack(int size){
this.maxSize = size;
array = new int[size];
top = -1;
}
//压入数据
public void push(int value){
if(top < maxSize-1){
array[++top] = value;
}
}
//弹出栈顶数据
public int pop(){
return array[top--];
}
//访问栈顶数据
public int peek(){
return array[top];
}
//判断栈是否为空
public boolean isEmpty(){
return (top == -1);
}
/*//判断栈是否满了
public boolean isFull(){
return (top == maxSize-1);
}*/
}
class Solution {
public boolean isValid(String s) {
MyStack test1 = new MyStack(100000);
char[] arr = s.toCharArray();
for(int i=0;i<arr.length;i++){
switch(arr[i]){
case '(':
case '[':
case '{': test1.push(arr[i]);break;
case ')':
case ']':
case '}':
if(test1.isEmpty()){
return false;
}else{
char ch = (char)test1.peek();
if( (ch=='('&&arr[i]==')') || (ch=='['&&arr[i]==']') || (ch=='{'&&arr[i]=='}')){
test1.pop();
}else{
return false;
}
}
}
}
if(test1.isEmpty()){
return true;
}else{
return false;
}
}
}