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(Aconcatenated withB), whereAandBare valid strings, or - It can be written as
(A), whereAis 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 <= 1000Sonly 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 ...
随机推荐
- sql中问号是干什么的??
第一次在后台 程序中遇到sql语句中的问号: /** * * 方法描述 : 通过账号id更新该账号状态 * @param state 状态 * @param id 账号id */ @Modifying ...
- kali linux: 网卡监听及扫描网络
一.网卡监听: kali不支持内置网卡,仅仅支持usb网卡.我用的虚拟机.首先在虚拟机的可移动设备里打开usb网卡的连接,执行ifconfig命令,如下: eth0接口是本地有线网卡信息,lo接口是本 ...
- Java 面试知识点汇总
OOP:(Object Oriented Programming )面向对象编程 重用性.灵活性和扩展性 高内聚.低耦合 面向过程编程与面向对象编程的区别:举例,自己做饭吃与去饭馆吃,去饭馆只需要知道 ...
- etherboot无盘启动
2001.10.30 吴峰光 本站提供对无盘启动的支持.本文就此作一简单介绍. 一.概述 无盘启动,更确切的说是网络启动,可算是最为轻松和简便的启动方式了. 目前还很少有人了解它,因为目前的软硬件条件 ...
- 富文本编辑器和fastdfs的使用
宜立方商城的系统架构a) 功能介绍(项目架构,有哪些功能模块,这些功能模块如何实现?)b) 架构讲解工程搭建-后台工程c) 使用maven搭建工程(后台工程如何搭建?)d) 使用maven的tomca ...
- ubuntu配置ftp
0.sudo apt-get install vsftpd 1.vi /etc/vsftpd.conf 将里面的注释打开 2.sudo service vsftpd restart 3.端口号21
- avalonjs 笔记
1>复选卡框和单选框 复选卡框 监控已选框的数组,即通过属性监控来判断是否全选 <div ms-controller="test"> <ul> < ...
- WebAPI如何返回json
public HttpResponseMessage PostUser(User user) { JavaScriptSerializer serializer = new JavaScriptSer ...
- nodejs、webpack
开发环境,直接本机下载代码回来,装好nodejs.webpack(安装方法 npm install webpack -g), 切换到项目根目录下 1.安装依赖:npm install 2.执行webp ...
- String.getBytes()[转]
在Java中,String的getBytes()方法是得到一个操作系统默认的编码格式的字节数组.这个表示在不通OS下,返回的东西不一样! String.getBytes(String decode)方 ...