public class Solution {
public int longestValidParentheses(String s) {
Stack<Integer> stack = new Stack<Integer>();
StringBuilder sBuilder = new StringBuilder(s);
for (int i = 0; i < s.length(); i++) {
if (sBuilder.charAt(i) == ‘(‘) {
stack.push(i);
} else {
if (stack.isEmpty()) {
continue;
} else {
sBuilder.setCharAt(i, ‘a‘);
sBuilder.setCharAt(stack.pop(), ‘a‘);
}
}
}
int max = 0, len = 0;
for (int i = 0; i < sBuilder.length(); i++) {
if (sBuilder.charAt(i) == ‘a‘) {
len++;
max = Math.max(max, len);
} else {
len = 0;
}
}
return max;
}
}[LeetCode]Longest Valid Parentheses, 解题报告,布布扣,bubuko.com
[LeetCode]Longest Valid Parentheses, 解题报告
原文地址:http://blog.csdn.net/wzy_1988/article/details/25600581