当前位置:网站首页>【栈】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;
}
}边栏推荐
- [learning notes] double + two points
- Visual studio 2019 shortcut notes
- Solve idea:class' xxx 'not found in module' xxx‘
- Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)
- 一些本质的区别
- Installing mongodb database in Windows Environment
- Double position relay dls-5/2 dc220v
- 基础知识之三——标准单元库
- 解析创客教育实践中的智慧原理
- Mustache syntax
猜你喜欢
随机推荐
OCR的一些项目
fluttertoast
Kongyiji's first question: how much do you know about service communication?
个人博客搭建与美化
基础知识之三——标准单元库
DX-11Q信号继电器
StrictMode分析Activity泄漏-StrictMode原理(3)
Visual studio 2019 shortcut notes
StrictMode卡顿与泄漏检测-StrictMode原理(2)
探索互联网时代STEAM教育创新之路
Document service design
MFC TCP通信服务端客户端Demo备忘vs2019
Double position relay dls-5/2 dc220v
友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
Service
Service grid ASM year end summary: how do end users use the service grid?
解读创客教育所蕴含的科技素养
(学习力+思考力) x 行动力,技术人成长的飞轮效应总结
Interpreting the scientific and technological literacy contained in maker Education
二十多年来第一次!CVPR最佳学生论文授予中国高校学生!









