Leetcode longest valid parentheses

Source: Internet
Author: User
class Solution {public:    int longestValidParentheses(string s) {        vector<int> stack;        int maxlen = 0;        int curlen = 0;        int last = -1;        int len = s.length();        for (int i=0; i<len; i++) {            char ch = s[i];            if (ch == ‘(‘) {                stack.push_back(i);                continue;            }            if (stack.empty()) {                last = i;                continue;            }            stack.pop_back();            if (stack.empty()) {                curlen = i - last;            } else {                curlen = i - stack.back();            }                        if (curlen > maxlen) maxlen = curlen;        }                return maxlen;    }};

Reference: http://www.cnblogs.com/zhuli19901106/p/3547419.html

Last is used to record the position of the last unmatching right parenthesis. When the stack is empty, a valid sequence of parentheses is bound, and the start of the sequence must be a position after the last one.

I have a limited IQ. I have been doing this for a long time. I only have to look at the answer...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.