-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp32.rs
34 lines (31 loc) · 919 Bytes
/
p32.rs
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
#[test]
fn test() {
assert_eq!(longest_valid_parentheses("(()".to_string()), 2);
assert_eq!(longest_valid_parentheses(")()())".to_string()), 4);
assert_eq!(longest_valid_parentheses("".to_string()), 0);
}
// 栈
pub fn longest_valid_parentheses(s: String) -> i32 {
let mut stack: Vec<i32> = vec![];
stack.push(-1);
let mut ans: i32 = 0;
for (idx, ch) in s.chars().enumerate() {
// (,入栈
if ch == '(' {
stack.push(idx as i32);
}
// ),出栈
else {
stack.pop();
// 栈为空,说明是最后一个没有被匹配的右括号
if stack.is_empty() {
stack.push(idx as i32);
}
// 栈不为空,可以获得当前有效括号长度
else {
ans = ans.max(idx as i32 - stack.last().unwrap());
}
}
}
ans
}