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.

Approach #1: C++.[stack]

class Solution {
public:
int minAddToMakeValid(string S) {
int len = S.length();
if (len == 0) return 0;
stack<char> myStack;
myStack.push(S[0]); for (int i = 1; i < len; ++i) {
if (myStack.empty()) myStack.push(S[i]);
else if (myStack.top() == '(' && S[i] == ')') myStack.pop();
else myStack.push(S[i]);
} return myStack.size();
}
};

  

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. LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48

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

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

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

  6. [leetcode-921-Minimum Add to Make Parentheses Valid]

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

  7. AsyncTask 与 对话框显示 view.WindowManager$BadTokenException: Unable to add window…is not valid; is your a

    最近遇到一个奇葩的问题,好郁闷 之前也没有仔细看.问题偶尔出现一次.再去查看日志时,出现 view.WindowManager$BadTokenException: Unable to add win ...

  8. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  9. FB面经 Prepare: Make Parentheses valid

    给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...

随机推荐

  1. 从一个子视图或者一个View中刷新其他UITableView

    被问到了一个问题:如何从一个子视图或者一个View中刷新其他UITableView,常规的写法可能是这样的 TestTVC*testTVC =[[TestTVC alloc] init];[testT ...

  2. 深入探究jvm之类装载器

    一.class装载验证流程 1.加载 1).取得类的二进制流. 2).转为方法区数据结构. 3).在Java堆中生成对应的java.lang.Class对象. 2.链接--验证(目的:保证Class流 ...

  3. 平衡二叉树之RB树

    RB树(红黑树)并不追求“完全平衡”——它只要求部分地达到平衡要求,降低了对旋转的要求,从而提高了性能.由于它的设计,任何不平衡都会在三次旋转之内解决.典型的用途是实现关联数组(如C++中的map和s ...

  4. Jenkins 更新最新版本

    一般情况下,war的安装路径在/usr/share/jenkins目录下. 不过也有部分人不喜欢安装在这里,可以通过系统管理(System management)--> 系统信息(System ...

  5. Maven01 环境准备、maven项目结构、编译/测试/打包/清除、安装、

    0 前提准备 0.1 安装java开发环境 0.2 安装maven工具 1 maven项目基本结构 如图所示,整个maven项目有业务文件.测试文件.POM依赖管理文件:其实还有一个资源文件resou ...

  6. KVM下raw和qcow2格式磁盘文件IO测试

    1. Host OS 环境 CPU: Intel Xeon E5620 2.40GHz MEM: 16GB DISK: 500GB SATA OS: CentOS5.7 64bit 2. Guest ...

  7. 14-python登入教务网(python+bs4)

    用request先得到到session对象,用其去放送请求,会自动保存cookie. 模拟有验证码的登入步骤: 1.发送请求登入页面: 2.分析验证码的地址,以及要将登入请求发往的地址(可以先输入错的 ...

  8. Android有趣的全透明效果--Activity及Dialog的全透明(附android系统自带图标大全)[转]

    原文地址:http://blog.csdn.net/sodino/article/details/5822147 1.Activity全透明 同学zzm给了这个有趣的代码,现在公布出来. 先在res/ ...

  9. Microsoft(C)注册服务器(32位)CPU占用高

    Microsoft(C)注册服务器(32位)CPU占用高 摘自:https://blog.csdn.net/jtsqrj/article/details/83034252 2018年10月12日 23 ...

  10. 编写高质量代码改善C#程序的157个建议——建议71:区分异步和多线程应用场景

    建议71:区分异步和多线程应用场景 初学者有时候会将异步和多线程混为一谈.如果对它们之间的区别不是很清楚,很容易写出下面这样的代码: private void buttonGetPage_Click( ...