xxxxxxxxxx
class Solution {
public:
int longestValidParentheses(string s) {
}
};
xxxxxxxxxx
public class Solution {
public int LongestValidParentheses(string s) {
}
}
xxxxxxxxxx
/**
* @param {string} s
* @return {number}
*/
var longestValidParentheses = function(s) {
};
xxxxxxxxxx
# @param {String} s
# @return {Integer}
def longest_valid_parentheses(s)
end
xxxxxxxxxx
class Solution {
/**
* @param String $s
* @return Integer
*/
function longestValidParentheses($s) {
}
}
xxxxxxxxxx
public class Solution {
public int longestValidParentheses(String s) {
int left = 0, right = 0, maxlength = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
left++;
} else {
right++;
}
if (left == right) {
maxlength = Math.max(maxlength, 2 * right);
} else if (right >= left) {
left = right = 0;
}
}
left = right = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == '(') {
left++;
} else {
right++;
}
if (left == right) {
maxlength = Math.max(maxlength, 2 * left);
} else if (left >= right) {
left = right = 0;
}
}
return maxlength;
}
}