当前位置:网站首页>Leetcode 301. delete invalid parentheses
Leetcode 301. delete invalid parentheses
2022-07-27 22:05:00 【henujolly】
class Solution {
public:
vector<string>res;
vector<string> removeInvalidParentheses(string s) {
int lm=0,rm=0;
for(char c:s){
if(c=='('){
lm++;
}
else if(c==')'){
if(lm==0) rm++;
else lm--;
}
}
help(s, 0, lm,rm);
return res;
}
void help(string s,int start,int lm,int rm){
if(lm==0&&rm==0){
if(isvalid(s)){
res.push_back(s);
}
return;
}
for(int i=start;i<s.size();i++){
if(i!=start&&s[i]==s[i-1]) continue;
if (lm+ rm> s.size() - i) {
return;
}
if(lm>0&&s[i]=='('){
help(s.substr(0, i)+s.substr(i + 1), i, lm-1, rm);
}
else if(rm>0&&s[i]==')'){
help(s.substr(0, i)+s.substr(i + 1), i, lm, rm-1);
}
}
}
bool isvalid(string str){
int cnt=0;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '(') {
cnt++;
} else if (str[i] == ')') {
cnt--;
if (cnt < 0) {
return false;
}
}
}
return cnt == 0;
}
};
边栏推荐
- @Component可以和@Bean 用在同一个类上吗?
- 枚举和注解
- Enumeration and annotation
- 聊聊 MySQL 事务二阶段提交
- @Can component be used on the same class as @bean?
- Will the United States prohibit all Chinese enterprises from purchasing American chips? Trump responded like this
- Software testing interview question: what is regression testing?
- Pytoch distributed training
- [numerical analysis exercise] numerical integration (complex trapezoid, complex Simpson, Romberg integral) C with STL implementation
- Samsung's most advanced EUV production line has been put into operation: the 7Nm capacity this year will be three times that of last year
猜你喜欢

How to deal with high concurrency deadlock?

Implementation of arbitrary code execution based on.Net dynamic compilation technology

Why do server programs need to listen first

Learn the use principle and core idea of thread pool from the source code

The design idea of relational database is obvious to you in 20 pictures

Inertial navigation principle (VII) -imu error classification (II) -allan variance analysis method +imu test + calibration introduction
Excalidraw:很好用的在线、免费「手绘」虚拟白板工具

Search, insert and delete of hash table

How long will it take to learn the four redis cluster solutions? I'll finish it for you in one breath

8000 word explanation of OBSA principle and application practice
随机推荐
软件测试面试题:系统测试的策略有多少种?
Finish learning redis cluster solution at one go
Software testing interview question: what aspects should be considered when designing test cases, that is, which aspects should different test cases be tested for?
排序(冒泡排序)后面学习持续更新其它排序方法
How to customize logging of.Net6.0
day 1 - day 4
Log4j vulnerability is still widespread and continues to cause impact
Inertial navigation principle (VII) -imu error classification (II) -allan variance analysis method +imu test + calibration introduction
Mysql 数据恢复流程 基于binlog redolog undolog
软件测试面试题:什么是回归测试?
【海洋科学】海洋气候指数【Climate Indices】数据集
一种比读写锁更快的锁,还不赶紧认识一下
每条你收藏的资讯背后,都离不开TA
Software test interview question: suppose there is a text box that requires the input of a 10 character postal code, how should the text box be divided into equivalent classes?
成员方法及其传参机制
枚举和注解
8000字讲透OBSA原理与应用实践
Pythia: Facebook's latest open source visual and language multitasking learning framework
Encapsulate an array into a class
[C language] high precision addition, subtraction, multiplication and division template