Share my C# solution


  • 0
    D
    Stack<string> stack = new Stack<string>();
            string pre = s.Substring(0,1);
            for (int i = 0; i < s.Length; ++i)
            {
                stack.Push(s.Substring(i,1));
                if (stack.Count > 1)
                {
                    if (pre == "("&&stack.Peek()==")")
                    {
                        stack.Pop();
                        stack.Pop();
                    }
                    if (pre == "[" && stack.Peek() == "]")
                    {
                        stack.Pop();
                        stack.Pop();
                    }
                    if (pre == "{" && stack.Peek() == "}")
                    {
                        stack.Pop();
                        stack.Pop();
                    }
                }
    
                if (stack.Count != 0)
                {
                    pre = stack.Peek();
                }
            }
    
            if (stack.Count != 0)
            {
                return false;
            }
            else return true;

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.