【Longest Valid Parentheses】cpp

题目:

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

For "(()", the longest valid parentheses substring is "()", which has length = 2.

Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

代码:

复制代码
class Solution {
public:
    int longestValidParentheses(string s) {
        int global_longest = 0;
        int last_not_match = -1;
        stack<int> sta;
        for ( int i = 0; i < s.length(); ++i )
        {
            if ( s[i]=='(')
            {
                sta.push(i);
                continue;
            }
            if ( s[i]==')')
            {
                if ( sta.empty() )
                {
                    last_not_match = i;
                }
                else
                {
                    sta.pop();
                    if ( sta.empty() )
                    {
                        global_longest = std::max( global_longest, i-last_not_match );
                    }
                    else
                    {
                        global_longest = std::max( global_longest, i-sta.top() );
                    }
                }
            }
        }
        return global_longest;
    }
};
复制代码

tips:

这里巧用堆栈,核心是堆栈里存放的不是字符本身而是字符所在的索引值。

额外再用一个变量保存上一次没有匹配的字符的索引值。

每次更新global_longest的时候,都判断堆栈是否清空:

1. 如果堆栈清空了,则证明直到last_not_match之前的都是有效的字符串,所以完整有效长度为i-last_not_match

2. 如果堆栈没有清空,则说明还有‘(’没有被匹配上,所以临时有效长度i-sta.top()

这里设定last_not_match初始值为-1,主要是考虑如果从第一个元素就包含在有效范围内的test case (如,“()”)

=======================================

此题还有DP的解法,但总感觉DP解法思路不太直观,怪怪的。

留一个看过的DP解法的blog,以后有时间再研究

http://bangbingsyb.blogspot.sg/2014/11/leetcode-longest-valid-parentheses.html

=========================================

第二次过这道题,一开始写了一个动态规划的算法:逻辑可能是对的,但是绝对超时。

还是强化记忆了一下stack的做法:巧用栈的特点,O(n)复杂度搞定。

复制代码
class Solution {
public:
    int longestValidParentheses(string s) {
            stack<int> sta;
            int ret = 0;
            int last_not_match = -1;
            int i=0;
            while ( i<s.size() )
            {
                if ( s[i]=='(' )
                {
                    sta.push(i);
                }
                else
                {
                    if ( sta.empty() )
                    {
                        last_not_match = i;
                    }
                    else
                    {
                        sta.pop();
                        if (sta.empty())
                        {
                            ret = max(ret,i - last_not_match);
                        }
                        else
                        {
                            ret = max(ret, i - sta.top());
                        }
                    }
                }
                ++i;
            }
            return ret;
    }
};
复制代码

 

posted on   承续缘  阅读(137)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示