LeetCode 301. Remove Invalid Parentheses
原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/
题目:
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())()"]
")(" -> [""]
题解:
题目要求减少最小个数的非法括号,要求最小,就想到BFS.
Queue 里加入 原来的string s, 循环中从queue中poll出来一个string. 每次减掉一个括号,若是合法了就加到res中,同时停止继续enqueue, 只把queue中现有的一个level检查了就好。
若是没遇到合法的,就把str从头到尾减掉一个括号放回到queue中。
检查是否合法用isValid. 这种写法不需要stack, 节省了空间。
同时使用Set 来记录已经检查过的string, 检查过就不需要再次检查。
Time Complexity: exponential. n = s.length(). Space: O(n).
AC Java:
public class Solution {
public List<String> removeInvalidParentheses(String s) {
List<String> res = new ArrayList<String>();
if(s == null){
return res;
} LinkedList<String> que = new LinkedList<String>();
HashSet<String> visited = new HashSet<String>(); //存储已经检查过的string, 没有必要重复检查
que.add(s);
visited.add(s);
boolean found = false; //标签,检查是否找到了valid parentheses while(!que.isEmpty()){
String str = que.poll();
if(isValid(str)){
res.add(str);
found = true;
}
if(found){ //已经找到,就在本level中找剩下的就好了
continue;
}
for(int i = 0; i<str.length(); i++){
if(str.charAt(i) != '(' && str.charAt(i) != ')'){
continue;
}
String newStr = str.substring(0,i) + str.substring(i+1);
if(!visited.contains(newStr)){
que.add(newStr);
visited.add(newStr);
}
}
}
return res;
} private boolean isValid(String s){
int count = 0;
for(int i = 0; i<s.length(); i++){
if(s.charAt(i) == '('){
count++;
}
if(s.charAt(i) == ')'){
if(count == 0){
return false;
}else{
count--;
}
}
}
return count == 0;
}
}
类似Minimum Remove to Make Valid Parentheses.
LeetCode 301. Remove Invalid Parentheses的更多相关文章
- [LeetCode] 301. Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- [leetcode]301. Remove Invalid Parentheses 去除无效括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- 301. Remove Invalid Parentheses
题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...
- [LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- 301. Remove Invalid Parentheses去除不符合匹配规则的括号
[抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...
- 【leetcode】301. Remove Invalid Parentheses
题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...
- 301 Remove Invalid Parentheses 删除无效的括号
删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果.注意: 输入可能包含了除 ( 和 ) 以外的元素.示例 :"()())()" -> ["()()() ...
- Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)
Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
随机推荐
- 百度前端技术学院-task1.10源代码
任务十的源代码,其实有github的,但就是不知道怎么弄,近期会学会的.在IE和firefox上检测运行良好. <!DOCTYPE html> <html lang="en ...
- 【题解】物流运输 [ZJ2006] [P1772] [BZOJ1003]
[题解]物流运输 [ZJ2006] [P1772] [BZOJ1003] 传送门:物流运输 \([ZJ2006]\) \([P1772]\) \([BZOJ1003]\) [题目描述] 给定一个含 \ ...
- Java学习:内部类的概念于分类
内部类的概念于分类 如果一个事物的内部类包含另一个事物,那么这就是一个类内部包含另一个类.例如:身体和心脏的关系,又如:汽车和发动机的关系. 分类 成员内部类 局部内部类(包含匿名内部类) 成员内部类 ...
- MQTT --- Retained Message
保留消息定义 如果PUBLISH消息的RETAIN标记位被设置为1,则称该消息为“保留消息”: Broker会存储每个Topic的最后一条保留消息及其Qos,当订阅该Topic的客户端上线后,Brok ...
- drools -规则语法
文章结构 1. 基础api 2. FACT对象 3. 规则 4. 函数 1. 基础api 在 Drools 当中,规则的编译与运行要通过Drools 提供的各种API 来实现,这些API 总体来讲可以 ...
- css3中的calc的使用
最近在布局的时候遇到一个问题,在页面中的左侧是侧边栏,右边是内容区域,内容区域中有一个固定定位的标签页,在设置固定定位的标签设置宽度的时候应该是内容区域的宽度,而固定定位的时候相对于是窗口的宽度,所以 ...
- 89.canvas制作爱心
<!DOCTYPE html> <html> <head> <title>JavaScript和html53D玫瑰花(程序员的情人节礼物)< ...
- 安全SECUERITY英文SECUERITY证券
security Alternative forms secuerity (mostly obsolete) English Alternative forms secuerity Pronuncia ...
- 推荐一些github上的免费好书
本文转载自公众号:跟着小一写bug. 熬夜等于慢性自杀,那熬夜和喜欢的人说话,算不算是慢性殉情? 晚上好 小一来啦 有木有想哀家 其实今晚小一有个拳击课 可是 由于项目明天要演示 调一 ...
- Ubuntu中wine程序安装windows软件中文乱码如何解决
1.安装wine sudo apt install wine 2.安装中文程序方法 下载exe文件 在命令行执行 wine 文件名.exe 3.中文乱码原因分析 查看/home/用户名/.wine/d ...