题解 | #有效括号序列#

有效括号序列

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;
    }
}

全部评论

相关推荐

zhch7:建议9✌️把学历加黑加粗,如果实在offer可能是觉得佬不会去
投了多少份简历才上岸
点赞 评论 收藏
分享
每晚夜里独自颤抖:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-25 17:22
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务