zoukankan      html  css  js  c++  java
  • [LeetCode] 921. Minimum Add to Make Parentheses Valid

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', and in any positions ) so that the resulting parentheses string is valid.

    Formally, a parentheses string is valid if and only if:

    • It is the empty string, or
    • It can be written as AB (A concatenated with B), where A and B are valid strings, or
    • It can be written as (A), where A is a valid string.

    Given a parentheses string, return the minimum number of parentheses we must add to make the resulting string valid.

    Example 1:

    Input: "())"
    Output: 1
    

    Example 2:

    Input: "((("
    Output: 3
    

    Example 3:

    Input: "()"
    Output: 0
    

    Example 4:

    Input: "()))(("
    Output: 4

    Note:

    1. S.length <= 1000
    2. S only consists of '(' and ')' characters.

    使括号有效的最少添加。

    给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的括号( '(' 或是 ')',可以在任何位置),以使得到的括号字符串有效。

    从形式上讲,只有满足下面几点之一,括号字符串才是有效的:

    它是一个空字符串,或者
    它可以被写成 AB (A 与 B 连接), 其中 A 和 B 都是有效字符串,或者
    它可以被写作 (A),其中 A 是有效字符串。
    给定一个括号字符串,返回为使结果字符串有效而必须添加的最少括号数。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/minimum-add-to-make-parentheses-valid
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这道题跟1541题类似,也是只能添加以满足所有括号能配对,思路是用stack。扫描 input 字符串,如果当前扫描到一个左括号,则把它入栈;如果当前扫描到一个右括号,看一下 stack.peek() 是否为一个左括号,若是,则可以将其 pop 以抵消;如果当前扫描到一个右括号但是stack为空,则说明没有左括号供其抵消,则 res++,说明需要一个额外的左括号。最后如果stack不为空,则再res += stack.size(),因为stack中剩下的都是左括号需要被配对。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int minAddToMakeValid(String s) {
     3         // corner case
     4         if (s == null || s.length() == 0) {
     5             return 0;
     6         }
     7 
     8         // normal case
     9         int res = 0;
    10         Stack<Character> stack = new Stack<>();
    11         for (int i = 0; i < s.length(); i++) {
    12             char c = s.charAt(i);
    13             if (c == '(') {
    14                 stack.push(c);
    15             } else if (c == ')') {
    16                 if (!stack.isEmpty() && stack.peek() == '(') {
    17                     stack.pop();
    18                 } else if (stack.isEmpty()) {
    19                     res++;
    20                 }
    21             }
    22         }
    23         res += stack.size();
    24         return res;
    25     }
    26 }

    相关题目

    20. Valid Parentheses

    678. Valid Parenthesis String

    1111. Maximum Nesting Depth of Two Valid Parentheses Strings

    921. Minimum Add to Make Parentheses Valid

    1541. Minimum Insertions to Balance a Parentheses String

    LeetCode 题目总结

  • 相关阅读:
    前后端项目结构规范性记录
    开发问题记录(这部分还是比较零碎)
    对HashMap的一次记录
    面试问题记录 三 (JavaWeb、JavaEE)
    面试问题记录 二 (数据库、Linux、Redis)
    面试问题记录 一 (基础部分)
    对正则表达式的一些记录
    WEB与游戏开发的一些区别
    MarkDown常用语法全纪录
    MySQL压测相关内容
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14153139.html
Copyright © 2011-2022 走看看