题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return bool布尔型 */ public bool isValid (string s) { // write code here if (string.IsNullOrWhiteSpace(s) || s.Length == 0) return true; List<char> listC = new List<char>() { s[0] }; for (int nIndex = 1; nIndex < s.Length; nIndex++) { char c = s[nIndex]; if (c == '(' || c == '[' || c == '{') listC.Add(c); else { if (listC.Count == 0) return false; if ((listC[listC.Count - 1] == '(' && c == ')') || (listC[listC.Count - 1] == '[' && c == ']') || (listC[listC.Count - 1] == '{' && c == '}')) listC.RemoveAt(listC.Count - 1); else break; } } return listC.Count > 0 ? false : true; } }