class Solution { public: int longestValidParentheses(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int n = s.length(); int maxl = 0; int count = 0; int len = 0; for(int i = 0;i < n;i++){ if(s[i] == '('){ count++; len++; } if(s[i] == ')') { count--; len++; } if(count == 0 && len > maxl){// one valid substring found maxl = len; } else if(count < 0){//invalid pre-fix len = 0; count = 0; } } return maxl; } };
n*a
7 楼
((())这个你返回几,应该是4; 还有这个 ()(),应该也返回4
【在 a*********0 的大作中提到】 : class Solution { : public: : int longestValidParentheses(string s) { : // Start typing your C/C++ solution below : // DO NOT write int main() function : int n = s.length(); : int maxl = 0; : int count = 0; : int len = 0; : for(int i = 0;i < n;i++){