【算法】C#实现判断括号互相嵌套的正确性问题

      //{[{}]([])}或[{()[]}]等为正确格式,而{[( ])}或({[()})等均为不正确的格式。
        public bool IsBracketMatch(string str)
        {
            Stack stack = new Stack();
            for (int i = 0; i < str.Length; i++)
            {
                switch (str[i])
                {
                    case '{':
                    case '[':
                    case '(':
                        stack.Push(str[i]);
                        break;
                    case '}':
                        if (stack.Count > 0 && stack.Pop() == '{')
                            break;
                        else
                            return false;
                    case ']':
                        if (stack.Count > 0 && stack.Pop() == '[')
                            break;
                        else
                            return false;
                    case ')':
                        if (stack.Count > 0 && stack.Pop() == '(')
                            break;
                        else
                            return false;
                    default:
                        return false;
                }
            }
            return stack.Count == 0;
        }

你可能感兴趣的:(算法,C#括号嵌套,括号嵌套算法,栈结构的应用)