public class Solution
    {
        public int MinAddToMakeValid(string S)
        {
            Stack<char> ST = new Stack<char>();
            foreach (var s in S)
            {
                if (s.Equals('('))
                {
                    ST.Push(s);
                }
                else//')'
                {
                    if (ST.Count > 0)
                    {
                        var c = ST.Peek();
                        if (c.Equals('('))
                        {
                            ST.Pop();
                        }
                        else
                        {
                            ST.Push(s);
                        }
                    }
                    else
                    {
                        ST.Push(s);
                    }
                }
            }
            int count = ST.Count;
            while (ST.Any())
            {
                var st = ST.Pop();
            }
            return count;
        }
    }

 

posted on 2018-10-14 19:54  Sempron2800+  阅读(109)  评论(0编辑  收藏  举报