题解 | #有效括号序列#

有效括号序列

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 17:13
想去,但是听说加班强度实在难崩,所以拒绝了,现在有点心梗对面hr感觉也是实习生,打电话的时候怪紧张的,但是感觉人很好嘞
水中水之下水道的鼠鼠:哥们这不先去体验一下,不行再跑呗,大不了混个实习经历(有更好的转正offer就当我没说)
点赞 评论 收藏
分享
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 11:47
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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