题解 | #最长的括号子串#

最长的括号子串

http://www.nowcoder.com/practice/45fd68024a4c4e97a8d6c45fc61dc6ad

class Solution {
public:
    /**
     * 
     * @param s string字符串 
     * @return int整型
     */
    int longestValidParentheses(string s) {
        // write code here
        stack<int> st;
        st.push(-1);
        
        int res = 0, cur = 0;
        while(cur < s.length()){
            if(s[cur] == '('){
                st.push(cur);
                cur++;
            }
            else{
                if(st.top() < 0 || s[st.top()] != '('){
                    st.push(cur);
                    cur++;
                    continue;
                }
                else{
                    st.pop();
                    res = max(res, cur-st.top());
                    cur++;
                }
            }
        }
        //res = max(res, cur-st.top()+1);
        return res;
    }
};
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务