classSolution { public: intlongestValidParentheses(string s){ int n = s.length(); int ans = 0;
for (int i = 0; i < n; i++) { if (s[i] == ')') continue; int cur = 0, temp = 0, llen = 1; for (int j = i + 1; j < n; j++) { if (s[j] == '(') llen++; else { if (llen > 0) { llen--; temp++; if (llen == 0) { cur += temp; temp = 0; } } else break; } ans = ans >= cur ? ans : cur; } }
classSolution { public: intlongestValidParentheses(string s){ int n = s.length(); int ans = 0; int cur = 0, temp = 0, llen = 0;
for (int i = 0; i < n; i++) { if (s[i] == '(') llen++; else { if (llen > 0) { llen--; temp++; if (llen == 0) { cur += temp; temp = 0; } } else { if (cur != 0) { ans = ans >= cur ? ans : cur; cur = 0; } } } }