Title longest effective brackets 32. leetcode

topic

Given a string containing only '(' and ''), find the length of the longest substring comprising an effective parentheses.

Examples

Example 1:

Input: "(()"
Output: 2
Explanation: the longest substring effective parentheses "()"

Example 2:

Input: ") () ())"
Output: 4
explained: the longest substring effective parentheses "() ()"

(Hereinafter problem solution ideas from leetcode official)

Use a stack of ideas

First -1 is added to the stack (-1 for calculating the length of the string, such as "(()))"). We used to start traversing the string, encountered "(" to push its index of encounter ")," directly out of the stack, then the current index minus the current top of the stack to get a value, compared to the max, stay the maximum one.

Examples algorithm (a ") () ())" as an example) Here Insert Picture Description:

Thinking a Code

public class problem32 {
	public int longestValidParentheses(String s) {
		int max=0;
		Stack<Integer> st=new Stack<Integer>();
		st.push(-1);
		
		for(int i=0;i<s.length();i++){
			if(s.charAt(i)=='('){
				st.push(i);
			}else{
				st.pop();
				if(st.isEmpty()){
					st.push(i);
				}else{
					max=Math.max(max, i-st.peek());
				}
			}
		}
		
		return max;
    }
	public static void main(String[] args) {
		problem32 pro=new problem32();
		System.out.println(pro.longestValidParentheses(")()())"));
	}
}

Two dynamic programming ideas

This problem can be solved by dynamic programming. We define a dp array, wherein the i-th element represented by the following denoted the longest effective length of the character substring of ending i. We will dp array all initialized to zero. Now, obviously effective in certain substring ')' end. This further conclusions can be drawn: the "(" value at dp array location corresponding to the end of the substring must be 0. So we just need to be updated. ")" Corresponding to the value of the position in the array dp.

The following is a solution process dp array:
. 1, S [I] = ")" && S [I-. 1] = "(" time, which is the form "... ()"
dp [I] = dp [I-2] +2;
wherein dp [i-2] represents the "(" in front of the effective length of the substring, 2 represents a new substring "()" in length

2, s [i] = " )" && s [i-1] = ")" When, that is shaped like "...))"
DP [I] = DP [I-. 1] + DP [I-DP [I -1] -2] + 2;
wherein dp [i-1] represents a ")" in front of the effective length of the substring, and 2 denotes substring new "()" in length, dp [i-dp [i-1] -2] indicates that this section:
this section ((...))

3, s [i] = "(", skipping

In fact, there is also a lot of the details of things, while some are considering the algorithm when it is already taken into account, but we did not notice, but also some deal with the problem of the border, the addition of these rules so that moving the process becomes very complicated, there is no join in, so it is best to slowly draw their own understanding of it.

Moving Regulation Code

public class problem32_2 {
	public int longestValidParentheses(String s) {
		int max = 0;
		int dp[] = new int[s.length()];

		for (int i = 1; i < s.length(); i++) {
			if (s.charAt(i) == ')') {
				if (s.charAt(i - 1) == '(') {
					dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2;
				} else if (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;
				}
			}
			max = Math.max(max, dp[i]);
		}

		return max;
	}
	public static void main(String[] args) {
		problem32_2 pro=new problem32_2();
		System.out.println(pro.longestValidParentheses("(()))"));
	}
}
Published 15 original articles · won praise 0 · Views 175

Guess you like

Origin blog.csdn.net/qq_36360463/article/details/104029439