-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestValidParentheses.java
More file actions
83 lines (80 loc) · 2.59 KB
/
LongestValidParentheses.java
File metadata and controls
83 lines (80 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package basic.QueueAndStack;
import java.util.Stack;
/**
* https://leetcode.com/problems/longest-valid-parentheses/
* 32. Longest Valid Parentheses
* 给定一个字符串,字符串中只有"("和")"两种字符,求有效括号的最大长度
* Example 1:
* <p>
* Input: s = "(()"
* Output: 2
* Explanation: The longest valid parentheses substring is "()".
* Example 2:
* <p>
* Input: s = ")()())"
* Output: 4
* Explanation: The longest valid parentheses substring is "()()".
* Example 3:
* <p>
* Input: s = ""
* Output: 0
*/
public class LongestValidParentheses {
/**
* 解法1
* The idea only update the result (max) when we find a "pair" and push -1 to stack first covered all edge cases.
*
* @param s
* @return
*/
public int longestValidParentheses(String s) {
Stack<Integer> stack = new Stack<>();
//覆盖边缘情况,如()
stack.push(-1);
int maxLen = 0;
for (int i = 0; i < s.length(); i++) {
//只有找到匹配的一对,才更新maxLen,其他情况下将不匹配的元素直接压栈,用来计算i-stack.peek()
if (s.charAt(i) == ')' && stack.size() > 1 && s.charAt(stack.peek()) == '(') {
stack.pop();
maxLen = Math.max(maxLen, i - stack.peek());
} else {
stack.push(i);
}
}
return maxLen;
}
/**
* 解法2
* 使用两个变量left和right,分别记录左括号和右括号的数量,遍历字符穿
* 如果左右括号数量相等,则更新最大数量
* 如果右括号数量大于左括号,如()),则重新开始计数
* 如果左括号数量大于右括号,如((),也重新开始计数
* 注:此种解法需要两个循环
*
* @param s
* @return
*/
public int longestValidParentheses2(String s) {
int left = 0, right = 0, maxLen = 0;
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i) =='(') {
++left;
}else {
++right;
}
if(left==right) maxLen=Math.max(maxLen,2*right);
if(right>left) left=right=0;
}
left=right=0;
for (int i=s.length()-1;i>=0;i--) {
if(s.charAt(i) =='(') {
++left;
}else {
++right;
}
if(left==right) maxLen=Math.max(maxLen,2*right);
if(left>right) left=right=0;
}
return maxLen;
}
}