zoukankan      html  css  js  c++  java
  • leetcode 32 Longest Valid Parentheses

    题意

    给一个由'(' 和 ')'构成的字符串,求最长匹配长度

    题解

    class Solution {
    public:
        int longestValidParentheses(string s) {
            int len = s.size();
            if (len == 0) return 0;
            stack<int> st;
            for (int i=0; i<len; i++) {
               if(s[i] == '(')
                   st.push(i);
                else if(s[i] == ')') {
                    if(!st.empty() && s[st.top()] == '(') {
                        st.pop();
                    } else {
                        st.push(i);
                    }
                }
            }
            if(st.empty()) return len;
            int mx = 0;
            int ed = len, start = 0;
            while(!st.empty()) {
                // cout << start << " ed";
                start = st.top(); st.pop();
                mx = max(mx, ed - start - 1);
                ed = start;
            }
            mx = max(mx, start);
            return mx;
        }
    };
    
  • 相关阅读:
    洛谷单元最短路标准版——spfa优化
    airline
    有依赖的背包
    挖地雷
    带分数问题
    子集和问题
    循环比赛日程表
    传纸条
    机器分配
    分组背包
  • 原文地址:https://www.cnblogs.com/Draymonder/p/11248750.html
Copyright © 2011-2022 走看看