[leetcode-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.
思路:
用一个栈即可,如果顶部和字符串中的当前字符匹配则出栈,最后栈的大小即为不匹配的个数。
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]的更多相关文章
- [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,如果是'('直接入栈 ...
- 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) 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 1249. Minimum Remove to Make Valid Parentheses
原题链接在这里:https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/ 题目: Given a string s ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
随机推荐
- Android攻城狮学习笔记-进阶篇一
点击快速抵达: 第1章 AndroidManifest配置文件 第2章 使用ListView显示信息列表 第3章 使用DatePicker及TimePicker显示当前日期和时间 第4章 使用Grid ...
- w10下Oracle 11g完全干净卸载
1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: 2.打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit> H ...
- BUAA OO 2019 第一单元作业总结
目录 总 架构 Controller Model 输入处理 代码静态分析 行数 方法复杂度 UML 类图 优点 缺点 坑 输入 非法的空白字符 输入的简并处理 运算 浅拷贝 可变类型与不可变类型 ...
- “C++动态绑定”相关问题探讨
一.相关问题: 1. 基类.派生类的构造和析构顺序 2. 基类.派生类中virtual的取舍 二.测试代码: #include <iostream> class A { public: A ...
- java面向对象之个人总结
面向对象有三大特性:继承,封装,多态 1.继承: (1)继承的特点:A,java支持单根继承,不支持多根继承 B,java支持多层继承(继承体系) (2)细节注意:A.子类只能继承父类的非私有成员(成 ...
- Phpstorm如何连接服务器
当服务器是Linux的时候不懂指令觉得很懊恼,这个时候直接就可以使用PHPstorm连接服务器操作了: 1丶准备工作 首先你先要准备服务器丶phpstorm这两个吧! 2丶开始配置phpstorm 按 ...
- Linux下onvif客户端获取ipc摄像头 获取能力:GetCapabilities
GetCapabilities:获取能力,主要目的获取设备能力信息(获取媒体服务地址) 鉴权:但是在调用获取设备能力之前是需要鉴权的.ONVIF协议规定,部分接口需要鉴权,部分接口不需要鉴权,在调用需 ...
- Go语言中的常量
1 概述 常量,一经定义不可更改的量.功能角度看,当出现不需要被更改的数据时,应该使用常量进行存储,例如圆周率.从语法的角度看,使用常量可以保证数据,在整个运行期间内,不会被更改.例如当前处理器的架构 ...
- 3、顺序表、内存、类型、python中的list
1.内存.类型本质.连续存储 1.内存本质 2.C 语言实例-计算 int, float, double 和 char 字节大小 使用 sizeof 操作符计算int, float, double 和 ...
- String与Date的互相转换
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 也可以: SimpleDateFormat sd ...