1021. 删除最外层的括号
This commit is contained in:
+11
-25
@@ -3,40 +3,26 @@ mod list;
|
|||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn min_insertions(s: String) -> i32 {
|
pub fn remove_outer_parentheses(s: String) -> String {
|
||||||
let s = s.as_bytes();
|
|
||||||
let mut emp_s = vec![];
|
|
||||||
let mut ans = 0;
|
|
||||||
let mut i = 0;
|
|
||||||
while i < s.len() {
|
|
||||||
if s[i] == b'(' {
|
|
||||||
emp_s.push(b'(');
|
|
||||||
} else {
|
|
||||||
if i + 1 >= s.len() || s[i + 1] != b')' {
|
|
||||||
ans += 1;
|
|
||||||
} else {
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
emp_s.push(b')');
|
|
||||||
}
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
let mut cnt = 0;
|
let mut cnt = 0;
|
||||||
for (i, c) in emp_s.iter().enumerate() {
|
let mut ans = vec![];
|
||||||
if *c == b'(' {
|
for c in s.chars() {
|
||||||
if cnt < 0 {
|
if c == '(' {
|
||||||
ans += -cnt;
|
if cnt != 0 {
|
||||||
cnt = 0;
|
ans.push(c);
|
||||||
}
|
}
|
||||||
cnt += 1;
|
cnt += 1;
|
||||||
} else {
|
} else {
|
||||||
cnt -= 1;
|
cnt -= 1;
|
||||||
|
if cnt != 0 {
|
||||||
|
ans.push(c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if cnt < 0 { ans - cnt } else { ans + cnt * 2 }
|
ans.iter().collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("{:?}", Solution::min_insertions("()()))((".to_string()));
|
println!("{:?}", Solution::remove_outer_parentheses("(()())(())(()(()))".to_string()));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user