1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_301.java 1.84 KB
一键复制 编辑 原始数据 按行查看 历史
Fisher Coder 提交于 6年前 . refactor 301
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
/**
* 301. 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())()"]
")(" -> [""]
*/
public class _301 {
public static class Solution1 {
public List<String> removeInvalidParentheses(String s) {
List<String> result = new ArrayList<>();
if (s == null) {
return result;
}
Set<String> visited = new HashSet();
Queue<String> q = new LinkedList();
q.offer(s);
visited.add(s);
boolean found = false;
while (!q.isEmpty()) {
String curr = q.poll();
if (isValid(curr)) {
found = true;
result.add(curr);
}
if (found) {
continue;//this means if the initial input is already a valid one, we'll just directly return it and there's actually only one valid result
}
for (int i = 0; i < curr.length(); i++) {
if (curr.charAt(i) != '(' && curr.charAt(i) != ')') {
continue;//this is to rule out those non-parentheses characters
}
String next = curr.substring(0, i) + curr.substring(i + 1);
if (!visited.contains(next)) {
q.offer(next);
visited.add(next);
}
}
}
return result;
}
private boolean isValid(String str) {
char[] chars = str.toCharArray();
int count = 0;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c == '(') {
count++;
}
if (c == ')') {
count--;
if (count == -1) {
return false;
}
}
}
return count == 0;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助