using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return bool布尔型 */ public bool isValid (string s) { Stack<char> sta = new Stack<char>(); for(int i = 0; i < s.Length; i++){ char cur = s[i]; if(cur == '(' || cur == '[' || cur == '{') { sta.Push(cur); continue; } if(cur == ')' && sta.Count != 0 && sta.Pop() == '(') continue; if(cur == ']' && sta.Count != 0 && sta.Pop() == '[') continue; if(cur == '}' && sta.Count != 0 && sta.Pop() == '{') continue; return false; } return sta.Count == 0; } }