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 ...
随机推荐
- hdu4778:状压dp+博弈
题目大意: 有g种不同颜色的小球,b个袋子,每个袋子里面有若干个每种小球 两人轮流取袋子,当袋子里面的同色小球有s个时,会合并成一个魔法球,并被此次取袋子的人获得 成功获得魔法球的人可以再次取 求二者 ...
- Display number of replies in disscussion board
how to display number of replies in disscussion board I have a require about display the replies' nu ...
- 关于js对象引用的小例子
看完下面的代码,相信对js对象引用又有了新的认识,直接上代码: // split()把字符串分割成字符串数组 // reverse() 会改变数组本身,**并返回原数组的引用**.!!!! var a ...
- autoitv3点击windows界面
在自动化测试过程中会遇到如下windows安全认证,需要输入账号和密码,这个认证对话框不属于element元素.无法用selenium操作,需要用autoitv3操作,输入账号密码后,再进行web元素 ...
- alert 在手机浏览器会显示网址,怎么能去掉这个网址
之前就看到有人发过这帖子,现在自己也遇到这问题了. 目前想到的一个解决方案,是用jquery的模拟的alert插件进行代替,可是找的几个插件都不能实现alert的阻塞功能.怎么破 ,具体解决方案如下: ...
- wcf简单的创建和运用
创建一个控制台应用程序,命名为wcftest,并在同一解决方案中添加一个wcf服务应用程序 在wcf项目中会自动生成Service1.svc服务程序文件和IService1.cs契约接口 IServi ...
- ListView的Item被点击和其中的Button被点击同时生效
Android开发中在ListView中经常有Button或ImageButton等需要被点击的控件,如果不加一些特殊的限制,有可能 ListView的Item的点击事件或Button的点击事件,其中 ...
- 你所不知道的java编程思想
读thinking in java这本书的时候,有这么一句话“在编译单元的内部,可以有一个公共(public)类,它必须拥有与文件相同的名字” 有以下疑问: 在一个类中说可以有一个public类,那是 ...
- (转)sql语句中charindex的用法
假如你写过很多程序,你可能偶尔会碰到要确定字符或字符窜串否包含在一段文字中,在这篇文章中,我将讨论使用CHARINDEX和PATINDEX函数来搜索文字列和字符串.我将告诉你这两个函数是如何运转的,解 ...
- easyUI的doCellTip 就是鼠标放到单元格上有个提示的功能
1:这个东西是我抄的(抄的哪儿的我就想不起来了- -)弹出的窗没有样式 不是很好看 //扩展 $.extend($.fn.datagrid.methods, { /** * 开打提示功能 * @pa ...