[Leetcode] Longest Valid Parentheses

找出字串裡最長的合法括號組

簡單說,一樣stack搜尋合法parenth,把不合法的 ( & ) index 紀錄下來,最後算index間的差值取最大就是最長的

 

public class Solution
    {
        /// <summary>
        /// 把不合法的'(' ')'的index記下來,取最長的差值就是
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public int LongestValidParentheses(string s)
        {
            char[] aaa = s.ToCharArray();
            int aaaLength = aaa.Length;
            s = null;
            //有剩下的'('就是不合法的
            Stack<int> notLeftMatched = new Stack<int>();
            //不合法的')'
            List<int> notMatched = new List<int>();
            int tempCount = 0;
            int resultCount = 0;
            int popCount = 0;
            int j = 0;
            for (int i = 0; i < aaa.Length; i++)
            {
                j = i;
                if (aaa[i] == '(')
                {
                    //存的是1為base的index
                    notLeftMatched.Push(++j);
                }
                else if (aaa[i] == ')' )
                {
                    //有'('可以配對
                    if(notLeftMatched.Count > 0)
                    {
                        notLeftMatched.Pop();
                        popCount++;
                    }
                    else
                    {
                        //沒有就表示是個斷點')' 記下來
                        notMatched.Add(++j);
                    }
                }
            }
            int[] all = new int[notLeftMatched.Count + notMatched.Count];
            notMatched.CopyTo(all);
            notLeftMatched.CopyTo(all, notMatched.Count);
            //不合法的'(' ')'組成一個陣列後排序
            all = all.OrderBy(x => x).ToArray();
            notLeftMatched = null;
            notMatched = null;
            aaa = null;
            int afterIndex = aaaLength;
            ///算斷點間的差值
            if (all.Length > 0 && popCount > 0)
            {
                for (int i = all.Length - 1; i >= 0; i--)
                {
                    //取得最後一個
                    if(i == all.Length - 1)
                    {
                        afterIndex = all[i];
                        continue;
                    }
                    //不包含自已 再扣1
                    //例如在1跟4之間合法的就只有2 3 所以是 4 -1 -1
                    tempCount = afterIndex - all[i] -1;
                    if (tempCount > resultCount)
                    {
                        resultCount = tempCount;
                    }
                    afterIndex = all[i];
                }
                //算第一個斷點跟最後一個斷點跟字元陣列頭尾的差值
                //例如長10 斷點是 2 5 7 那還有7-10 , 1-2之間的長度要算
                //算尾
                tempCount = aaaLength - all[all.Length -1];
                if (tempCount > resultCount)
                {
                    resultCount = tempCount;
                }

                //算頭
                tempCount = all[0] - 1;
                if (tempCount > resultCount)
                {
                    resultCount = tempCount;
                }
            }
            else
            {
                resultCount = popCount * 2;
            }

            all = null;

            return resultCount;
        }
    }

 

posted on 2019-04-09 00:55  seako  阅读(102)  评论(0编辑  收藏  举报