当前位置:网站首页>【栈】921. Minimum Add to Make Parentheses Valid
【栈】921. Minimum Add to Make Parentheses Valid
2022-07-01 00:41:00 【暮色_年华】
A parentheses string is valid if and only if:
It is the empty string,
It can be written as AB (A concatenated with B), where A and B are valid strings, or
It can be written as (A), where A is a valid string.
You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.
For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".
Return the minimum number of moves required to make s valid.
题意:添加括号最少个数使括号合法。
有效括号的等价条件:
(1)“(”“)”个数相等
(2)任意前缀中左括号的个数大于等于右括号的个数
class Solution {
public int minAddToMakeValid(String s) {
int l=0,r=0;
for(char c:s.toCharArray()){
if(c=='(')l++;
else{
if(l==0)r++;
else l--;
}
}
return l+r;
}
}边栏推荐
猜你喜欢
随机推荐
sort自定义函数
Green, green the reed. dew and frost gleam.
Analyzing the wisdom principle in maker education practice
StrictMode带来的思考-StrictMode原理(5)
解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
DLS-42/6-4 DC110V双位置继电器
解读创客教育所蕴含的科技素养
ASCII、Unicode、GBK、UTF-8之间的关系
双位置继电器DLS-5/2 DC220V
【Qt5-基础篇】随机数显示屏展示
Interpreting the scientific and technological literacy contained in maker Education
1175. Prime Arrangements
Typora的使用
Analyze the maker education path integrating the essence of discipline
自定义注解实现校验
Exploration and practice of "flow batch integration" in JD
【Qt5-基础篇_1】从0开始,德天老师和你一起学习——窗口简介
DLS-20型双位置继电器 220VDC
软硬件基础知识学习--小日记(1)
Split the linked list [take next first and then cut the linked list to prevent chain breakage]








