示例 1:
输入: "(()"
输出: 2
解释: 最长有效括号子串为 "()"
思路:加入辅助数组,左括号直接进栈,辅助数组对应编号置0,右括号进看前面是否有配对,有左括号配对,将前面的左括号出栈,将当前右和左括号在辅助数组中对应位置置1,最后看连续的1的最大数返回。
class Solution {
public int longestValidParentheses(String s) {
List<ele> list = new ArrayList<ele>();
int max = 0;
int now = 0;
int size = s.length();
int[] tmp = new int[size];//辅助数组
int i = 0;//辅助数组下标
for(char c : s.toCharArray()){
if(c == '('){
list.add(new ele(i,c));
tmp[i] = 0;
i++;
continue;
}
if(c==')'){
if(list.isEmpty()||list.get(list.size()-1).getSmp() != '(') {
now = 0;
tmp[i] = 0;
i++;
continue;
}
else{
ele e = list.get(list.size() - 1);
tmp[e.getIndex()] = 1;
tmp[i] = 1;
i++;
list.remove(list.size()-1) ;
}
}
}
//return max;
for(int x=0;x<tmp.length;x++){
if(tmp[x] == 1){
now += 1;
max = max > now?max:now;
}
else{
now = 0;
}
}
return max;
}
public static class ele{ //辅助类
public int index;
public char smp;
public ele(int index,char smp){
this.index = index;
this.smp = smp;
}
public int getIndex(){
return this.index;
}
public char getSmp(){
return this.smp;
}
}
}