当前位置:网站首页>921. Minimum additions to make parentheses valid
921. Minimum additions to make parentheses valid
2022-07-06 16:08:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/minimum-add-to-make-parentheses-valid/
subject :
Given a by '(' and ')' A string of parentheses S, We need to add at least parentheses ( '(' or ')', Can be anywhere ), To make the resulting parenthesis string valid .
In form , Only meet one of the following , Bracket strings are valid :
It's an empty string , perhaps
It can be written as AB (A And B Connect ), among A and B Are valid strings , perhaps
It can be written (A), among A Is a valid string .
Given a bracket string , Returns the minimum number of parentheses that must be added to make the result string valid .
Example 1:
| Input :"())" Output :1 |
Example 2:
| Input :"(((" Output :3 |
Example 3:
| Input :"()" Output :0 |
Example 4:
| Input :"()))((" Output :4 |
Tips :
| S.length <= 1000 S Contains only '(' and ')' character . |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/minimum-add-to-make-parentheses-valid
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
The given string has only Left parenthesis , Right bracket , If the quantity matches, it doesn't mean the expected result
such as :)))(((
Because I met Right bracket
If there is no left parenthesis in front , You must add left parentheses to match
If there is an open parenthesis in front , Then subtract one from the number of left parentheses
Meet the left bracket , We need the following characters to see if there is , So count the next number
Finally back to Left parenthesis , Right bracket ( We use it to calculate the new quantity ) The sum of
Method 1 、 Traversal Statistics
int minAddToMakeValid(char * s){
int lnum = 0, rnum = 0;
int i = 0;
while(s[i])
{
if(s[i] == '(')
lnum++;
else
{
if(lnum > 0)
{
lnum--;
}
else
rnum++;
}
i++;
}
return lnum + rnum;
}边栏推荐
- [exercise-9] Zombie's Treasury test
- 基于web的照片数码冲印网站
- Data storage in memory & loading into memory to make the program run
- 渗透测试 ( 4 ) --- Meterpreter 命令详解
- Interval sum ----- discretization
- [exercise-7] (UVA 10976) fractions again?! (fraction split)
- [exercise-7] crossover answers
- E. Breaking the Wall
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
- MySQL授予用户指定内容的操作权限
猜你喜欢

信息安全-安全编排自动化与响应 (SOAR) 技术解析

Nodejs+vue online fresh flower shop sales information system express+mysql

渗透测试 ( 8 ) --- Burp Suite Pro 官方文档

Web based photo digital printing website

渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
![[exercise-5] (UVA 839) not so mobile (balance)](/img/8e/48dcf75f7347b36301df6fc129c09d.png)
[exercise-5] (UVA 839) not so mobile (balance)

D - function (HDU - 6546) girls' competition

PySide6 信号、槽

X-forwarded-for details, how to get the client IP

渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
随机推荐
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
Nodejs crawler
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
Alice and Bob (2021 Niuke summer multi school training camp 1)
JS call camera
628. Maximum product of three numbers
Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
The most complete programming language online API document
Frida hook so layer, protobuf data analysis
Gartner:关于零信任网络访问最佳实践的五个建议
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
Shell Scripting
Research Report on market supply and demand and strategy of China's earth drilling industry
Understand what is a programming language in a popular way
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
【练习-6】(PTA)分而治之
Interesting drink
最全编程语言在线 API 文档
b站 实时弹幕和历史弹幕 Protobuf 格式解析