标签:
我的错误代码:
1 #include<iostream> 2 3 using namespace std; 4 5 int longestValidParentheses(string s) 6 { 7 int i = 0; 8 int L = s.length(); 9 int j; 10 int max = 0; 11 while (i < L) 12 { 13 int left = 0; 14 int right = 0; 15 if (s[i] == ‘(‘) 16 { 17 left++; 18 j = i + 1; 19 int index = i-1; 20 while (j<L&&left >= right) 21 { 22 if (left == right) 23 index = j-1; 24 if (s[j] == ‘(‘) 25 left++; 26 else 27 right++; 28 j++; 29 } 30 if (right >= left) 31 { 32 if (left*2>max) 33 max = left * 2; 34 i = j; 35 } 36 else 37 { 38 if (index == i - 1) 39 ; 40 else 41 { 42 int leftSum = index - i + 1; 43 int rightSum = j - i - (left - right) - leftSum; 44 if (leftSum > max) 45 max = leftSum; 46 if (rightSum > max) 47 max = rightSum; 48 if (leftSum > rightSum) 49 i = index; 50 else 51 i = j; 52 } 53 } 54 } 55 i++; 56 } 57 return max; 58 } 59 60 int main() 61 { 62 string s = "(()(((()"; 63 cout << longestValidParentheses(s) << endl; 64 }
Leetcode Longest Valid Parentheses
标签:
原文地址:http://www.cnblogs.com/chaiwentao/p/4494071.html