LeetCode 301. Remove Invalid Parentheses(删除无效的括号)
来源:互联网 发布:音乐剪切合并软件 mac 编辑:程序博客网 时间:2024/05/19 19:33
原题网址: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())()"]")(" -> [""]
方法一:广度优先搜索。
public class Solution { public List<String> removeInvalidParentheses(String s) { Set<String> visit = new HashSet<>(); List<String> results = new ArrayList<>(); LinkedList<String> queue = new LinkedList<>(); queue.add(s); boolean succ = false; while (!queue.isEmpty()) { String q = queue.remove(); if (succ) match(q, results); else succ = match(q, visit, queue, results); } return results; } private void match(String s, List<String> results) { // System.out.printf("Try matching %s\n", s); int match = 0; for(int i=0; i<s.length(); i++) { if (s.charAt(i) == '(') match ++; else if (s.charAt(i) == ')') match --; if (match < 0) return; } if (match == 0) results.add(s); // System.out.printf("Try matching %s successfully\n", s); } private boolean match(String s, Set<String> visit, LinkedList<String> queue, List<String> results) { // System.out.printf("Try matching %s\n", s); int match = 0; for(int i = 0; i < s.length(); i ++) { if (s.charAt(i) == '(') match ++; else if (s.charAt(i) == ')') match --; if (match < 0) { for(int j=0; j<=i; j++) { if (s.charAt(j) != ')') continue; String r = s.substring(0, j) + s.substring(j+1); if (visit.contains(r)) continue; visit.add(r); queue.add(r); } // System.out.printf("Try matching %s fail, queue=%s\n", s, queue); return false; } } if (match == 0) { results.add(s); // System.out.printf("Try matching %s successfully\n", s); return true; } match = 0; for(int i = s.length()-1; i >= 0; i --) { if (s.charAt(i) == ')') match ++; else if (s.charAt(i) == '(') match --; if (match < 0) { for(int j=s.length()-1; j>=i; j--) { if (s.charAt(j) != '(') continue; String r = s.substring(0, j) + s.substring(j+1); if (visit.contains(r)) continue; visit.add(r); queue.add(r); } // System.out.printf("Try matching %s fail, queue=%s\n", s, queue); return false; } } // System.out.printf("Try matching %s fail, queue=%s\n", s, queue); return false; }}
public class Solution { private Set<String> visit = new HashSet<>(); private List<String> results = new ArrayList<>(); private void removeLeft(String s) { int match = 0; for(int i=s.length()-1; i>=0; i--) { if (s.charAt(i) == ')') match ++; else if (s.charAt(i) == '(') match --; if (match < 0) { for(int j=s.length()-1; j>=i; j--) { if (s.charAt(j) != '(') continue; String r = s.substring(0, j) + s.substring(j+1); if (visit.contains(r)) continue; visit.add(r); removeLeft(r); } return; } } results.add(s); } private void removeRight(String s) { int match = 0; for(int i=0; i<s.length(); i++) { if (s.charAt(i) == '(') match ++; else if (s.charAt(i) == ')') match --; if (match < 0) { for(int j=0; j<=i; j++) { if (s.charAt(j) != ')') continue; String r = s.substring(0, j) + s.substring(j+1); if (visit.contains(r)) continue; visit.add(r); removeRight(r); } return; } } if (match == 0) { results.add(s); return; } removeLeft(s); } public List<String> removeInvalidParentheses(String s) { removeRight(s); return results; }}
方法三:先删除无效的右括号,再删除无效的左括号,深度优先搜索。
这道题我没有做出好的解法,参考:http://algobox.org/remove-invalid-parentheses/
精妙之处在于:使用lastj保存上一次发现不匹配的位置和上一次删除括号的位置。
但如何证明唯一性?这个我还没有搞懂。
public class Solution { private void removeLeft(String s, int lastI, int lastJ, List<String> rs) { int match = 0; for(int i=lastI; i<s.length() && i>=0; i--) { if (s.charAt(i) == ')') match ++; else if (s.charAt(i) == '(') match --; if (match >= 0) continue; for(int j=lastJ; j<s.length() && j>=i; j--) { if (s.charAt(j) != '(') continue; if (j==lastJ || s.charAt(j+1) != '(') removeLeft(s.substring(0, j) + s.substring(j+1, s.length()), i-1, j-1, rs); } return; } rs.add(s); } private void removeRight(String s, int lastI, int lastJ, List<String> rs) { int match = 0; for(int i=lastI; i<s.length(); i++) { if (s.charAt(i) == '(') match ++; else if (s.charAt(i) == ')') match --; if (match >= 0) continue; for(int j=lastJ; j<=i; j++) { if (s.charAt(j) != ')') continue; if (j==lastJ || s.charAt(j-1) != ')') removeRight(s.substring(0, j) + s.substring(j+1, s.length()), i, j, rs); } return; } removeLeft(s, s.length()-1, s.length()-1, rs); } public List<String> removeInvalidParentheses(String s) { List<String> results = new ArrayList<>(); removeRight(s, 0, 0, results); return results; }}
带注释:
public class Solution { private void removeLeft(String s, int matchTo, int removeTo, List<String> results) { int matched = 0; for(int m=matchTo; m>=0; m--) { if (s.charAt(m) == ')') matched ++; else if (s.charAt(m) == '(') matched --; if (matched >= 0) continue; for(int r=removeTo; r>=m; r--) { //检查是左括号才能删除 if (s.charAt(r) != '(') continue; if (r==removeTo || s.charAt(r+1) != '(') removeLeft(s.substring(0, r)+s.substring(r+1), m-1, r-1, results); } //如果本次有删除,则留待后面加入到results return; } results.add(s); } private void removeRight(String s, int matchTo, int removeTo, List<String> results) { int matched = 0; for(int m=matchTo; m<s.length(); m++) { if (s.charAt(m) == '(') matched ++; else if (s.charAt(m) == ')') matched --; if (matched >= 0) continue; for(int r=removeTo; r<=m; r++) { //检查是右括号才能删除 if (s.charAt(r) != ')') continue; if (r==removeTo || s.charAt(r-1) != ')') removeRight(s.substring(0, r)+s.substring(r+1), m, r, results); } //如果本次有删除,则留待后面加入到results return; } removeLeft(s, s.length()-1, s.length()-1, results); } public List<String> removeInvalidParentheses(String s) { List<String> results = new ArrayList<>(); removeRight(s, 0, 0, results); return results; }}
1 0
- LeetCode 301. Remove Invalid Parentheses(删除无效的括号)
- 【Leetcode】301. Remove Invalid Parentheses 移除非法小括号
- leetcode 301. Remove Invalid Parentheses BFS遍历得到所有合法的括号字符串
- 301. Remove Invalid Parentheses 去掉不合理的括号
- Leetcode 301. Remove Invalid Parentheses
- leetcode 301. Remove Invalid Parentheses
- [leetcode] 301. Remove Invalid Parentheses
- leetcode.301. Remove Invalid Parentheses
- [LeetCode]--301. Remove Invalid Parentheses
- Leetcode 301. Remove Invalid Parentheses
- 【LeetCode】301. Remove Invalid Parentheses
- [LeetCode]301. Remove Invalid Parentheses
- Leetcode 301. Remove Invalid Parentheses
- leetcode 301. Remove Invalid Parentheses
- [leetcode]301. Remove Invalid Parentheses
- Leetcode: 301.Remove Invalid Parentheses
- [LeetCode] Remove Invalid Parentheses
- LeetCode -- Remove Invalid Parentheses
- 自定义扇形进度条、异步加载网络图片、Activity疯狂旋转动画
- Android 可拖拽的GridView效果实现, 长按可拖拽和item实时交换
- zstack之应用层的发送函数及绑定(CC2530)
- HBase集群配置
- 推荐系统笔记一、基于近邻的推荐系统(基础篇)
- LeetCode 301. Remove Invalid Parentheses(删除无效的括号)
- 推荐系统笔记二、矩阵分解协同过滤
- 推荐系统笔记三、基于近邻的推荐系统进阶篇
- Python-回文检测和布尔操作(and,or,not)
- 散列学习(二)
- 跟黄哥学python序列文章之python方法链(method chaining)
- raft实现
- sklearn画ROC曲线
- eclipse、ddms、android studio连接不上手机问题解决