当前位置:网站首页>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;
}
边栏推荐
- X-forwarded-for details, how to get the client IP
- C basic grammar
- 基于web的照片数码冲印网站
- 树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
- mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
- 渗透测试 ( 1 ) --- 必备 工具、导航
- Opencv learning log 28 -- detect the red cup cover
- Interesting drink
- Nodejs+vue online fresh flower shop sales information system express+mysql
- HDU - 6024 Building Shops(女生赛)
猜你喜欢
C language is the watershed between low-level and high-level
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
Penetration test (1) -- necessary tools, navigation
Information security - Analysis of security orchestration automation and response (soar) technology
滲透測試 ( 1 ) --- 必備 工具、導航
渗透测试 ( 1 ) --- 必备 工具、导航
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Nodejs+vue网上鲜花店销售信息系统express+mysql
随机推荐
初入Redis
[exercise 4-1] cake distribution
Research Report on market supply and demand and strategy of China's earth drilling industry
Borg Maze (BFS+最小生成树)(解题报告)
【高老师软件需求分析】20级云班课习题答案合集
树莓派4B安装opencv3.4.0
Find 3-friendly Integers
Flink 使用之 CEP
STM32 learning record: LED light flashes (register version)
Data storage in memory & loading into memory to make the program run
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
[exercise-7] crossover answers
STM32 how to use stlink download program: light LED running light (Library version)
Borg maze (bfs+ minimum spanning tree) (problem solving report)
China potato slicer market trend report, technical dynamic innovation and market forecast
PySide6 信号、槽
Interesting drink
Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
对iptables进行常规操作
[exercise-6] (UVA 725) division = = violence