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泛型学习二

    通配符的使用 上一篇中知道,Box<Number>和Box<Integer>实际上都是Box类型,现在需要继续探讨一个问题,那么在逻辑上,类似于Box<Number> ...

  2. Python基础-画图:matplotlib

    Python画图主要用到matplotlib这个库.具体来说是pylab和pyplot这两个子库.这两个库可以满足基本的画图需求. pylab神器:pylab.rcParams.update(para ...

  3. 初识Qt涂鸦板绘制

    1.新建一个Qt Gui应用,项目名称为myPalette,基类选择为QMainWindow,类名设置为MainWindow. 2.在mainwindow.h头文件中添加以下代码,同时添加#inclu ...

  4. sql sever和mysql 卸载及oracle安装

    sql sever和mysql的卸载及Oracle安装 目的:本人健忘,以后难免会重装系统啥的,软件卸了装是常有的事,特此写此详细教程,一是方便自己以后重装的时候可以看看:二是如果有某位初学者有幸光临 ...

  5. Apache24 + php5.6.31 +Sql server R2 环境搭建①

    win8(7)x64系统下 :PHP5.5.15 + Apache2.4.10 + SQL server 2008 R2  的配置方法分享给大家,32位的同理,不过下载的软件需要也是32位的. 好久未 ...

  6. Hive--可执行SQL的Hadoop数据仓库管理工具

    Hive是一个基于HDFS的数据仓库软件,可理解为数据库管理工具:Hive的功能主要有: 1. 支持使用SQL对分布式存储的大型数据集进行读.写.管理,将SQL转化成MapReduce任务执行: 2. ...

  7. go语言的结构体指针

    Go 语言结构体 Go 语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型.   结构体是由一系列具有相同类型或不同类型的数据构成的数据集合.   结构体表示一项记录,比 ...

  8. zlib库的编译及使用

    * 打开网址http://zlib.net/ 下载zlib源码, * 解压压缩包,进入目录:C:\Users\Administrator\Desktop\zlib-1.2.11\zlib-1.2.11 ...

  9. R语言文本挖掘+词云显示(jiebaR包+wordcloud2包)

    利用2018年政府工作报告的例子向大家展示一下R语言如何进行文本挖掘的~用到的包有jiebaR和wordcloud2. 1.安装并加载jiebaR install.packages("jie ...

  10. 【转】WCF扩展系列 - 行为扩展(Behaviors)

    原文:https://www.cnblogs.com/Creator/archive/2011/05/21/2052687.html 这个系列的第一部分将会重点关注WCF行为(behaviors),W ...