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.

思路:

用一个栈即可,如果顶部和字符串中的当前字符匹配则出栈,最后栈的大小即为不匹配的个数。

int minAddToMakeValid(string S) {
if(S.length()<=)return S.length();
stack<char>st;
for(int i = ; i < S.length(); i++)
{
if(S[i] == ')' && !st.empty() && st.top() == '(')
{
st.pop();
continue;
}
else
{
st.push(S[i]);
}
}
return st.size();
}

[leetcode-921-Minimum Add to Make Parentheses Valid]的更多相关文章

  1. [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  2. 【LeetCode】921. Minimum Add to Make Parentheses Valid 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  3. 【leetcode】921. Minimum Add to Make Parentheses Valid

    题目如下: 解题思路:上周都在忙着参加CTF,没时间做题,今天来更新一下博客吧.括号问题在leetcode中出现了很多,本题的解题思路和以前的括号问题一样,使用栈.遍历Input,如果是'('直接入栈 ...

  4. 921. Minimum Add to Make Parentheses Valid

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  5. LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48

    921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...

  6. [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  7. LeetCode 1249. Minimum Remove to Make Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/ 题目: Given a string s ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

随机推荐

  1. java.lang.NoClassDefFoundError: org/apache/ibatis/mapping/DatabaseIdProvider

    我用的方案是:maven+struts2+spring+mybatis 出现上述错误的原因是: <dependency>            <groupId>org.myb ...

  2. [译]新的CCSDS图像压缩推荐标准

    摘要——空间数据系统咨询委员会(CCSDS)的数据压缩工作组最近通过了图像数据压缩议案,最终版本预计在2005年发布.议案中采用的算法由两部分组成,先是一个对图像的二维离散小波变换,然后是对变换后的数 ...

  3. java 输出流 outputstream

    一:输入和输出概念 输入流(inputstream):对于java程序来说,从程序写入文件叫做输出. 输出流(outputstream):对于java程序来说,从文件读取数据,到java程序叫做输入. ...

  4. 如何使用eclipse运行简单的java程序

    打开eclipse,选择“file——new——Java project”   为我们的java项目取一个名字,然后点击完成.   这时候左侧列表就有了我们刚才新建的java项目,点开项目,在src目 ...

  5. Verilog HDL与C语言的比较

    Verilog HDL与C语言的比较 Verilog HDL是在C语言的基础上发展起来的,因而它保留了C语言所独有的结构特点.   为便于对Verilog HDL有个大致的认识,在这里将它与C语言的异 ...

  6. linux学习第十九天(iscsi配置)

    一.iSCSI 服务部署网络存储 服务器配置 添加硬盘,创建分区 l[root@localhost Desktop]# ls /dev/sd*  (系统下查看硬盘信息) /dev/sda  /dev/ ...

  7. Linux学习笔记(第十二章)

    grep进阶 grep:以整行为单位进行截取 截取的特殊符号 正规表示法特殊字符 注意: sed用法 格式化打印 awk 用法 diff档案对比: path旧文档升级为新文档

  8. IOLI crackme分析——从应用中学习使用radare2

    Crackme0x00 - writeup 我现在开始看radare2book了,现在刚看1/3,有些无聊,因为之前也看过一些radare2的实例讲解,所以现在先试着做一下里面的crackme练习. ...

  9. 4515: [Sdoi2016]游戏

    4515: [Sdoi2016]游戏 链接 分析: 树链剖分 + 超哥线段树.注意细节. 代码: #include<cstdio> #include<algorithm> #i ...

  10. C# 代码备份数据库 ,不需要 其他 DLL

    protected void Button1_Click(object sender, EventArgs e)    {        ///        ///备份方法        ///  ...