leetcode 32. Longest Valid Parentheses

三种解法

dp

publicclassSolution{publicintlongestValidParentheses(Strings){intmaxans=0;intdp[]=newint[s.length()];for(inti=1;i=2?dp[i-2]:0)+2;}elseif(i-dp[i-1]>0&&s.charAt(i-dp[i-1]-1)=='('){dp[i]=dp[i-1]+((i-dp[i-1])>=2?dp[i-dp[i-1]-2]:0)+2;}maxans=Math.max(maxans,dp[i]);}}returnmaxans;}}

你可能感兴趣的:(leetcode 32. Longest Valid Parentheses)