leet-code/src/main/java/leetcode/editor/cn/MinimumAddToMakeParenthesesValid.java
2021-04-29 23:21:52 +08:00

92 lines
2.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定一个由 '(' 和 ')' 括号组成的字符串 S我们需要添加最少的括号 '(' 或是 ')',可以在任何位置),以使得到的括号字符串有效。
//
// 从形式上讲,只有满足下面几点之一,括号字符串才是有效的:
//
//
// 它是一个空字符串,或者
// 它可以被写成 AB A 与 B 连接), 其中 A 和 B 都是有效字符串,或者
// 它可以被写作 (A),其中 A 是有效字符串。
//
//
// 给定一个括号字符串,返回为使结果字符串有效而必须添加的最少括号数。
//
//
//
// 示例 1
//
// 输入:"())"
//输出1
//
//
// 示例 2
//
// 输入:"((("
//输出3
//
//
// 示例 3
//
// 输入:"()"
//输出0
//
//
// 示例 4
//
// 输入:"()))(("
//输出4
//
//
//
// 提示:
//
//
// S.length <= 1000
// S 只包含 '(' 和 ')' 字符。
//
//
//
// Related Topics 栈 贪心算法
// 👍 84 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//921:使括号有效的最少添加
public class MinimumAddToMakeParenthesesValid {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumAddToMakeParenthesesValid().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minAddToMakeValid(String S) {
int length = S.length();
if (length == 0) {
return 0;
}
int num = 0;
Stack<Character> stack = new Stack<>();
for (char ch : S.toCharArray()) {
if (ch == ')') {
if (stack.isEmpty()) {
num++;
} else {
stack.pop();
}
} else {
stack.push(ch);
}
}
while (!stack.isEmpty()){
num++;
stack.pop();
}
return num;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}