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 解答的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)

    Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...

  2. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  3. Remove Invalid Parentheses

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  4. 301. Remove Invalid Parentheses

    题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...

  5. [Swift]LeetCode301. 删除无效的括号 | Remove Invalid Parentheses

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  6. [leetcode]301. Remove Invalid Parentheses 去除无效括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  7. LeetCode301. Remove Invalid Parentheses

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  8. 301. Remove Invalid Parentheses去除不符合匹配规则的括号

    [抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...

  9. [LeetCode] 301. Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

随机推荐

  1. hdu5045:带权二分图匹配

    题目大意 : n个人 做m道题,其中 每连续的n道必须由不同的人做 已知第i人做出第j题的概率为pij,求最大期望 思路:考虑每连续的n道题 都要n个人来做,显然想到了带权的二分图匹配 然后就是套模板 ...

  2. 浅谈c语言代码段 数据段 bss段

    代码段.数据段.bss段 (1)编译器在编译程序的时候,将程序中的所有的元素分成了一些组成部分,各部分构成一个段,所以说段是可执行程序的组成部分. (2)代码段:代码段就是程序中的可执行部分,直观理解 ...

  3. Java 学习第一天

    java 学习路线 http://edu.csdn.net/main/studyline/heimaline.html?flz java 学习视频 —— 马士兵:毕向东

  4. 【Linux】linux经常使用基本命令

    Linux中很多经常使用命令是必须掌握的,这里将我学linux入门时学的一些经常使用的基本命令分享给大家一下,希望能够帮助你们. 这个是我将鸟哥书上的进行了一下整理的,希望不要涉及到版权问题. 1.显 ...

  5. OC-字典&数组运用实例:通讯录的实现

    需求实现: 一.定义联系⼈类ContactPerson 实例变量:姓名.性别.电话号码.住址.分组名称. 方法:初始化⽅方法(姓名.电话号码).显⽰示联系⼈信息 二.定义AddressBook类, 封 ...

  6. ASE中的主要数据库

    Adaptive Server包括多种类型数据库: 必需数据库. “附加功能”数据库 .例子数据库 .应用数据库 1.必需数据库 master 数据库包含系统表,这些系统表中存储的数据被用来管理,有 ...

  7. 当chm文档点击左侧,右侧无内容时的解决方案

    右击chm文件->属性->安全选项卡,选择你登陆计算机的用户名,把权限改成完全控制就可以显示了

  8. 提高jQuery执行效率

    1. 使用最新版本的jQuery jQuery的版本更新很快,你应该总是使用最新的版本.因为新版本会改进性能,还有很多新功能. 下面就来看看,不同版本的jQuery性能差异有多大.这里是三条最常见的j ...

  9. vi 快捷键【转】【weber整理必出精品】

    光标的移动 命令 光标移动 h或^h 向左移一个字符 j或^j或^n 向下移一行 k或^p 向上移一行 l或空格 向右移一个字符 G 移到文件的最后一行 nG 移到文件的第n行 w 移到下一个字的开头 ...

  10. 委托,匿名方法,Lambda,泛型委托,表达式树

    一.委托:完成一个委托应分三个步骤://step01:首先用delegate定义一个委托;public delegate int CalculatorAdd(int x, int y);//step0 ...