[Leetcode]Remove Invalid Parentheses
来源:互联网 发布:java怎么用正则表达式 编辑:程序博客网 时间:2024/05/15 07:39
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
Note: The input string may contain letters other than the parentheses (
and)
.
Examples:
"()())()" -> ["()()()", "(())()"]"(a)())()" -> ["(a)()()", "(a())()"]")(" -> [""]
class Solution {public: void dfs(string &s,vector<string>&result,string str,int l,int r,int cnt,int start){ if(l == r && l==cnt){ for(int i = start;i < s.size();i++){ if(s[i] != '(' && s[i] != ')') str.append(1,s[i]); } for(int k = 0;k < result.size();k++){ if(str == result[k])return; } result.push_back(str); return; } for(int i = start;i < s.size();i++){ if(s[i]=='('){ if(l < cnt)dfs(s,result,str+'(',l+1,r,cnt,i+1); }else if(s[i]==')'){ if(l > r)dfs(s,result,str+')',l,r+1,cnt,i+1); }else{ str.append(1,s[i]); dfs(s,result,str,l,r,cnt,i+1); } } } /*algorithm: DFS */ vector<string> removeInvalidParentheses(string s) { //compute minium pair count int l = 0,r = 0; for(int i = 0;i < s.size();i++){ if(s[i]=='(')l++; else if(s[i]==')'&&l>r)r++; } vector<string>result; dfs(s,result,"",0,0,min(l,r),0); return result; }};
0 0
- [LeetCode] Remove Invalid Parentheses
- LeetCode -- Remove Invalid Parentheses
- leetcode Remove Invalid Parentheses
- LeetCode Remove Invalid Parentheses
- [Leetcode]Remove Invalid Parentheses
- leetcode:Remove Invalid Parentheses
- leetcode - Remove Invalid Parentheses
- 【Leetcode】Remove Invalid Parentheses
- LeetCode:Remove Invalid Parentheses
- Remove Invalid Parentheses -- Leetcode
- [LeetCode]Remove Invalid Parentheses
- leetcode 301 : Remove Invalid Parentheses
- Leetcode 301. Remove Invalid Parentheses
- leetcode 301. Remove Invalid Parentheses
- [leetcode] 301. Remove Invalid Parentheses
- leetcode.301. Remove Invalid Parentheses
- [LeetCode]--301. Remove Invalid Parentheses
- Leetcode 301. Remove Invalid Parentheses
- struts2中拦截器与过滤器的区别
- 第五章 简单的数据查询
- java web学习(基础篇)三 神奇的豆子——JavaBean
- Java开发常用的Linux命令
- 数据归一化处理
- [Leetcode]Remove Invalid Parentheses
- KL25 Signal Multiplexing and Pin Assignments
- 说说JSON和JSONP,也许你会豁然开朗
- 设计模式之--单例模式
- poj1159
- 第六章 聚合函数与分组
- windows api实现图片的镂空效果
- 算术移位与逻辑移位
- 指针和const