921. Minimum Add to Make Parentheses Valid
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 withB
), whereA
andB
are valid strings, or - It can be written as
(A)
, whereA
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:
S.length <= 1000
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的更多相关文章
- [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- 【LeetCode】921. Minimum Add to Make Parentheses Valid 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- 【leetcode】921. Minimum Add to Make Parentheses Valid
题目如下: 解题思路:上周都在忙着参加CTF,没时间做题,今天来更新一下博客吧.括号问题在leetcode中出现了很多,本题的解题思路和以前的括号问题一样,使用栈.遍历Input,如果是'('直接入栈 ...
- LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48
921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...
- [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- [leetcode-921-Minimum Add to Make Parentheses Valid]
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- AsyncTask 与 对话框显示 view.WindowManager$BadTokenException: Unable to add window…is not valid; is your a
最近遇到一个奇葩的问题,好郁闷 之前也没有仔细看.问题偶尔出现一次.再去查看日志时,出现 view.WindowManager$BadTokenException: Unable to add win ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- FB面经 Prepare: Make Parentheses valid
给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...
随机推荐
- Hibernate中Session与本地线程绑定
------------------siwuxie095 Hibernate 中 Session 与本地线程绑定 1.Session 类似于 JDBC 的连接 Connection 2.Session ...
- CentOS 6.4一键自动化安装ISO镜像光盘
下载CentOS-6.4-x86_64-minimal.iso 1 http://mirrors.163.com/centos/6.4/isos/x86_64/CentOS-6.4-x86_64-mi ...
- Setuptool+pip安装
https://pypi.python.org/pypi/setuptools 1. 下载ez_setup.py文件,cmd进入安装目录: 2. python setup.py install htt ...
- Linux ssldump命令
一.简介 tcpdump是一款很强大.很有用的网络侦听软件,但是对于ssl加密的数据包就无能为力了:ssldump则是一款可以侦听ssl加密的数据包的软件. 二.安装 1)通过yum安装 yum ...
- LoadRunner11学习记录六 -- 服务器分析
LoadRunner运行时,怎么利用服务器的一些参数进行分析: 1.内存分析方法 内存分析方法主要是用于判断系统有无遇到内存瓶颈,是否需要通过增加内存等手段提高系统性能表现.主要计数器包括Memory ...
- ORACLE B-TREE(B树)索引
内容简介: 1.普通B-TREE 索引; 2.唯一B-TREE 索引; 3.复合索引; ORACLE 默认的索引类型为B-TREE 索引,表中的行标识符(ROWID)和行相关的列值被存储在一个平衡树的 ...
- Perl 学习笔记-子程序
1.定义子程序 使用sub关键字定义 ; 子程序名和标识符同要求, 但是有的特殊的可以用 &符号; 子程序是全局的, 不需要再使用前声明; 重名函数后者覆盖前者. sub roger{ ...
- 免秘钥oracel官方下载jdk
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...
- Error creating bean with name 'as' defined in class path resource
(1)一个是setter方法的名字和配置文件对应名字有问题 (2)导入的包不对,搭建环境出错.
- bt协议详解 基础篇(上)
bt协议详解 基础篇(上) 最近开发了一个免费教程的网站,产生了仔细了解bt协议的想法,所以写了这一篇文章,后续还会写一些关于搜索和索引的东西,都是在开发这个网站的过程中学习到的技术,敬请期待. 1 ...