Remove Invalid Parentheses 解答
Question
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())()"]
")(" -> [""]
Solution
看到parenthese的问题,第一反应是用栈。这题要求minimum number,所以想到用BFS遍历解空间树。
思路为:
层次依次为删除0个元素,1个元素,2个元素。。。
层次遍历所有的可能。如果有一种可能是valid,那么不再遍历下面的层。
public class Solution {
public List<String> removeInvalidParentheses(String s) {
Set<String> visited = new HashSet<String>();
List<String> result = new ArrayList<String>();
List<String> current = new ArrayList<String>();
List<String> next;
current.add(s);
boolean reached = false;
// BFS
while (!current.isEmpty()) {
next = new ArrayList<String>();
for (String prev : current) {
visited.add(prev);
// If valid
if (isValid(prev)) {
reached = true;
result.add(prev);
}
// If not reached, then delete
if (!reached) {
for (int i = 0; i < prev.length(); i++) {
char tmp = prev.charAt(i);
if (tmp != '(' && tmp != ')') {
continue;
}
String newStr = prev.substring(0, i) + prev.substring(i + 1);
if (!visited.contains(newStr)) {
next.add(newStr);
visited.add(newStr);
}
}
}
}
if (reached) {
break;
}
current = next;
}
return result;
}
private boolean isValid(String s) {
Deque<Integer> stack = new LinkedList<Integer>();
for (int i = 0; i < s.length(); i++) {
char cur = s.charAt(i);
if (cur != '(' && cur != ')') {
continue;
}
if (cur == '(') {
stack.push(i);
}
if (cur == ')') {
if (stack.isEmpty()) {
return false;
} else {
stack.pop();
}
}
}
return stack.isEmpty();
}
}
事实上,我们可以不用栈,用一个count来检测是否是valid parenthese.
private boolean isValid(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char cur = s.charAt(i);
if (cur != '(' && cur != ')') {
continue;
}
if (cur == '(') {
count++;
}
if (cur == ')') {
count--;
if (count < 0) {
return false;
}
}
}
return count == 0;
}
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 ...
- 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 ...
- [Swift]LeetCode301. 删除无效的括号 | 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 ...
- LeetCode301. 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. Retu ...
- [LeetCode] 301. Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
随机推荐
- 二分-poj-3685-Matrix
题目链接: http://poj.org/problem?id=3685 题目大意: 有n*n的矩阵,第i行第j列的数为Aij= i2 + 100000 × i + j2 - 100000 × j + ...
- Android 百度地图API(01)_开发环境 HelloBaiduMap
转载于:http://blog.csdn.net/lmj623565791/article/details/37729091 转载于:http://blog.csdn.net/crazy1235/ar ...
- Windows下文件列举,搜索
Windows下列举文件用的函数是 FindFirstFile 和 FindNextFile ,另外一个结构体是WIN32_FIND_DATA 以下是MSDN对于WIN32_FIND_DATA的定义 ...
- Dynamics CRM 常用 JS 方法集合
JS部分 拿到字段的值 var value= Xrm.Page.getAttribute("attributename").getValue(); Xrm.Page.getAttr ...
- Android Xfermode 实战 实现圆形、圆角图片
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42094215,本文出自:[张鸿洋的博客] 1.概述 其实这篇本来准备Androi ...
- 调用百度地图API实现手机自动定位 (逆地址解析)
//声明地址解析器 var geoc = new BMap.Geocoder(); //自动定位 var autoLocation = function () { if (navigator.geol ...
- [C#][Database]C#通过ODBC以自定义端口连接数据库
数据库端的配置暂且不说,比较简单,新建用户并开启相应连接权限即可. 通过ODBC连接数据库,重点在于Connection String的书写,在此可以查到几乎所有类型的Data Server的Conn ...
- 介绍 Visifire 常用属性的设置
转载自http://www.cnblogs.com/xinyus/p/3422198.html 主要介绍 Visifire 常用属性的设置,用来生成不同样式的图例 设置Chart的属 //设置titl ...
- exc_bad_access(code=1, address=0x789870)野指针错误
原因: exc_bad_access(code=1, address=0x789870)野指针错误,主要的原因是,当某个对象被完全释放,也就是retainCount,引用计数为0后.再去通过该对象去调 ...
- 把python项目部署到centos里
.安装centos VMware9下面安装centos .在centos下面设置共享文件夹为你本地的论坛的代码,然后设置网络为桥接:直接连接到物理网络,赋值网络连接状态 .进入forum_svr.py ...