当前位置:网站首页>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-6] (UVA 725) division = = violence
- 初入Redis
- Opencv learning log 28 -- detect the red cup cover
- Hdu-6025-prime sequence (girls' competition)
- [exercise-9] Zombie's Treasury test
- 628. Maximum product of three numbers
- Path problem before dynamic planning
- Determine the Photo Position
- Opencv learning log 16 paperclip count
- b站 实时弹幕和历史弹幕 Protobuf 格式解析
猜你喜欢
1010 things that college students majoring in it must do before graduation
滲透測試 ( 1 ) --- 必備 工具、導航
Write web games in C language
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
【高老师UML软件建模基础】20级云班课习题答案合集
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
D - Function(HDU - 6546)女生赛
【高老师软件需求分析】20级云班课习题答案合集
Ball Dropping
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
随机推荐
1010 things that college students majoring in it must do before graduation
CS zero foundation introductory learning record
[exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
Record of brushing questions with force deduction -- complete knapsack problem (I)
Hdu-6025-prime sequence (girls' competition)
Find 3-friendly Integers
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
Opencv learning log 29 -- gamma correction
The concept of C language array
HDU - 6024 Building Shops(女生赛)
Penetration test (8) -- official document of burp Suite Pro
Opencv learning log 19 skin grinding
Borg Maze (BFS+最小生成树)(解题报告)
E. Breaking the Wall
Opencv learning log 28 -- detect the red cup cover
基于web的照片数码冲印网站
Research Report of peripheral venous catheter (pivc) industry - market status analysis and development prospect prediction
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
SSM框架常用配置文件
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)