Longest Valid Parentheses(Dynamic Programming)

题目地址:https://leetcode.com/problems/longest-valid-parentheses/

Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. For “(()”, the longest valid parentheses substring is “()”, which has length = 2.Another example is “)()())”, where the longest valid parentheses substring is “()()”, which has length = 4.

public class Solution {(String s) {Stack<Integer> stack = new Stack<Integer>();int max = 0;int from = -1;char[] S = s.toCharArray();for(int i = 0;i < s.length(); ++i){if(S[i]=='(‘) stack.push(i);{stack.pop();if(stack.isEmpty()) max = Math.max(max,i-from);else max = Math.max(max,i-stack.peek());}}}return max;}}

,我就想是一只草原中被牧童遗忘的羊,

Longest Valid Parentheses(Dynamic Programming)

相关文章:

你感兴趣的文章:

标签云: