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. Python中numpy.random.randn()与rand(),numpy.zeros、ones、eye

    转自:https://blog.csdn.net/u010758410/article/details/71799142

  2. 东芝线阵CCD芯片TCD1305DG驱动时序设计

    最近在做微型光谱仪,用到了东芝的CCD芯片TCD1305DG,该芯片是单行3648像素,输出信号是时间上离散的模拟信号,典型输出速率为0.5M,即每2000ns输出一个像素值(模拟信号),芯片内部集成 ...

  3. 对ArrayList存放的对象,按照对象的某个属性进行排序。

    使用Collections.sort()方法进行实现: import java.util.ArrayList; import java.util.Collections; import java.ut ...

  4. 一个牛公司的关于oracle数据的面试题

    我也忘记是从哪里download的了,为了加深记忆,也为了完成我的博客,我决定写进博客里,如果有错误,欢迎大家指正,谢谢. 问题一. 解释一下sum的作用? 答:求符合条件的记录某数值字段的和. 问题 ...

  5. UML类图6种主要关系区别和联系

    UML类图关系图示,因为长得都很类似,所以大家总会混淆,本文主要目的就是分析一下6种主要的关系,找到联系与区别,便于记忆. 6种主要的关系如图1所示.继承与实现.组合与聚合.关联与依赖可分别划分为一组 ...

  6. vue watch关于对象内的属性监听

    vue可以通过watch监听data内数据的变化.通常写法是: data: { a: 100 }, watch: { a(newval, oldVal) { // 做点什么... console.lo ...

  7. C++程序设计--实验二

    第二次实验主要内容是函数重载,快速排序及其模板实现,简单的user类实现. 实验结论: 一.函数重载编程练习 /*编写重载函数add(),实现对int型,double型和Complex型数据的加法.在 ...

  8. 20155236范晨歌_exp6信息搜集与漏洞扫描

    20155236范晨歌_exp6信息搜集与漏洞扫描 目录 实践目标 信息搜集 漏洞扫描 总结 实践目标 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口 ...

  9. 洛谷 P3795 钟氏映射

    洛谷 P3795 钟氏映射 题目背景 2233年,CSSYZ学校的数学老师兼数学竞赛顾问钟JG已经2200+岁啦! 为了庆生,他或她给广大人民群众出了道题. 题目描述 设集合N=M={x∣x∈N+​, ...

  10. 柯朗微积分与数学分析习题选解(1.2 节 d)

    一直在读<陶哲轩实分析>,陶的书非常的严谨,环环相扣,但是也有个缺点就是计算性的例子和应用方面的例子太少了.所以就又找了本柯朗的<微积分与数学分析>搭配着看.柯朗的书的习题与陶 ...