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. RESTFUL 概念

    1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...

  2. 微信图片反防盗链 用js不太成熟的解决方式 仅供参考

    $("#imgDiv img").each(function () { var img = $(this); var img_src = img.attr("src&qu ...

  3. cdoj31-饭卡(card) (01背包)

    http://acm.uestc.edu.cn/#/problem/show/31 饭卡(card) Time Limit: 3000/1000MS (Java/Others)     Memory ...

  4. [hdu4347]The Closest M Points(平衡树式kdtree)

    解题关键:模板题(结合起来了) #include<iostream> #include<cstdio> #include<cstring> #include< ...

  5. Linux的作业管理

    一.作业管理的场景 作业管理(job control)是在bash环境下使用的,主要使用在同一个bash中管理多个作业的场景,譬如登录bash之后想同时复制文件.数据搜索,编译. 但是bash的作业管 ...

  6. securecrt免密码登录

    一.前言 1. 环境 客户端系统:win7 securecrt版本:6.0.2 服务端系统:centos6.5 服务端ssh实现:openssh 2. 关于认证方式 我们知道ssh一般都会提供多种客户 ...

  7. DevCloud for CloudStack Development

    Apache CloudStack development is not an easy task, for the simplest of deployments one requires a se ...

  8. centos 命令行和图形桌面模式的切换

    1.安装系统时建议安装图形界面,毕竟图形桌面下安装程序,比较方便 2.系统部署完成后可以切换到命令行界面:打开一个SHELL窗口运行 init 3 即可进入命令行界面.恢复图形用init 5 3.进入 ...

  9. Docker for mac安装

    Mac安装Docker docker下载地址: https://hub.docker.com/editions/community/docker-ce-desktop-mac docker for m ...

  10. Spring Boot☞ 使用velocity渲染web视图

    效果图: 代码 <!DOCTYPE html><html><head lang="en"> <meta charset="UTF ...