
有点复杂https://www.bilibili.com/video/BV1ehGjzJEiq/?spm_id_from333.337.search-card.all.clickvd_source757bb2dbaa360a4764731a4b0a025637class Solution { public int longestValidParentheses(String s) { int[] len new int[s.length()]; char[] ch s.toCharArray(); int res 0; for(int i 1;i ch.length;i){ if(ch[i] )){ if((i - 1) 0 ch[i - 1] (){ len[i] (i - 2) 0 ? len[i - 2] 2 : 2; }else if((i - 1) 0 ch[i - 1] )){ int m i - len[i - 1] - 1; if(m 0 ch[m] (){ len[i] (m - 1) 0 ? 2 len[i -1] len[m - 1] : 2 len[i - 1]; } } res Math.max(res,len[i]); } } return res; } }