using System;
using System.Collections.Generic;
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return bool布尔型
*/
public bool isValid (string s) {
// write code here
Stack<char> st=new Stack<char>();
foreach(var c in s)
{
if(c=='(' ||c=='{' || c=='[')
st.Push(c);
if(st.Count==0)
return false;
if(c==')' && st.Pop()!='(')
return false;
if(c=='}' && st.Pop()!='{')
return false;
if(c==']' && st.Pop()!='[')
return false;
}
return st.Count==0;
}
}
括号匹配时典型的栈的用法,每次一个后括号出现,必须在栈顶部消去一个同类型的前括号,否则括号出错