Leetcode214: Remove Invalid Parentheses
2016-01-04 21:07
288 查看
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
Examples:
有待思考。。。。。。
Note: The input string may contain letters other than the parentheses
(and
).
Examples:
"()())()" -> ["()()()", "(())()"] "(a)())()" -> ["(a)()()", "(a())()"] ")(" -> [""]
class Solution { public: void search(string& s, unordered_set<string>& res_set, string& temp, int curr, int open, int s_sz, int& max_valid_len, bool left_deleted){ // dfs if(s_sz-curr<open) return; if(curr == s_sz){ if(temp.length()>=max_valid_len && res_set.find(temp) == res_set.end()){ max_valid_len = temp.length(); res_set.insert(temp); } return; } if(s[curr] != '(' && s[curr] != ')'){ temp += s[curr]; search(s, res_set, temp, curr+1, open, s_sz, max_valid_len, left_deleted); temp.pop_back(); return; } if(s[curr] == '('){ temp += "("; search(s, res_set, temp, curr+1, open+1, s_sz, max_valid_len, left_deleted); temp.pop_back(); search(s, res_set, temp, curr+1, open, s_sz, max_valid_len, true); } else{ if(!(s[curr] == ')' && open==0)){ temp += ')'; search(s, res_set, temp, curr+1, open-1, s_sz, max_valid_len, left_deleted); temp.pop_back(); } if(!left_deleted){ search(s, res_set, temp, curr+1, open, s_sz, max_valid_len, left_deleted); } } } vector<string> removeInvalidParentheses(string s) { vector<string> res; int sz = s.length(); unordered_set<string> res_set; string temp = ""; int max_valid_len = 0; search(s, res_set, temp, 0, 0, sz, max_valid_len, false); for(string a:res_set) res.push_back(a); return res; } };
有待思考。。。。。。
相关文章推荐
- iOS: 在UIViewController 中添加Static UITableView
- 为什么我们的web前端变的越来越复杂
- 【总结】C#中的数组
- 活动组件(三):Intent
- js计算一个时间加上n天后得到的时间
- gnuradio的Python应用中绘图
- 数据结构与算法——用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回
- Swift 实现单例模式Singleton pattern的三种方法
- KMP算法
- 保存数据到数据库
- python 代码片段20
- ubuntu mysql 常用命令
- 在Android Studio中部署arcgis for android
- Ojbect类的equals和hashCode方法
- 【bzoj2594】[Wc2006]水管局长数据加强版 link cut tree
- 第五章(使用对象) 编程题一
- mfc的定时器函数
- netty4源码解读
- LeetCode Letter Combinations of a Phone Number
- 关系型数据库管理系统简介