首页 > 技术文章 > leetcode921

asenyang 2018-10-14 19:54 原文

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

 

推荐阅读