leetcode 301. Remove Invalid Parentheses BFS遍历得到所有合法的括号字符串
来源:互联网 发布:app数据统计 编辑:程序博客网 时间:2024/05/21 10:25
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())()”]
“)(” -> [“”]
这道题很简单,就是去除不合理的左括号,或者右括号,做一个BFS广度优先遍历即可。
我是在网上看到这道题的教程,发现做法真的很棒,还有一种DFS+回溯的做法,这个也很不错,但是看不懂,所以这里我选择了BFS的做法。
需要注意的是判断括号有效性的做法值得学习。
代码如下:
import java.util.ArrayList;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Queue;import java.util.Set;/* * 类似广度优先遍历的做法 * 还有一个DFS的做法,但是看不懂 * * 如果是要求的到所有的合法的子串,那么 * */class Solution { public List<String> removeInvalidParentheses(String s) { List<String> res=new ArrayList<>(); if(s==null) return res; Set<String> visit=new HashSet<>(); Queue<String> queue=new LinkedList<>(); queue.add(s); visit.add(s); boolean found=false; while(queue.isEmpty()==false) { String top=queue.poll(); if(isVaild(top)) { res.add(top); found=true; } if(found) continue; for(int i=0;i<top.length();i++) { if(top.charAt(i)=='(' || top.charAt(i)==')') { String one=top.substring(0,i)+top.substring(i+1); if(visit.contains(one)==false) { queue.add(one); visit.add(one); } } } } return res; } public boolean isVaild(String s) { int count=0; for(int i=0;i<s.length();i++) { if(s.charAt(i)=='(') count++; else if(s.charAt(i)==')') count--; if(count<0) return false; } return count==0; } /* * 获取所有的合法的子串 * */ public List<String> getAllValidParentheses(String s) { List<String> res=new ArrayList<>(); if(s==null) return res; Set<String> visit=new HashSet<>(); Queue<String> queue=new LinkedList<>(); queue.add(s); visit.add(s); while(queue.isEmpty()==false) { String top=queue.poll(); if(isVaild(top)) res.add(top); else { for(int i=0;i<top.length();i++) { if(top.charAt(i)=='(' || top.charAt(i)==')') { String one=top.substring(0,i)+top.substring(i+1); if(visit.contains(one)==false) { queue.add(one); visit.add(one); } } } } } return res; }}
下面是C++的做法,就是BFS遍历所有可能的情况,然后逐个处理即可,
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>using namespace std;class Solution {public: vector<string> res; vector<string> removeInvalidParentheses(string s) { set<string> visit; queue<string> que; que.push(s); visit.insert(s); bool find = false; while (que.empty() == false) { string top = que.front(); que.pop(); if (isValid(top)) { res.push_back(top); find = true; } if (find == true) continue; else { for (int i = 0; i < top.length(); i++) { if (top[i] == '(' || top[i] == ')') { string tt = top.substr(0, i) + top.substr(i + 1); if (visit.find(tt) == visit.end()) { que.push(tt); visit.insert(tt); } } } } } return res; } bool isValid(string a) { int count = 0; for (char c : a) { if (c == '(') count++; else if (c == ')') count--; if (count < 0) return false; } return count == 0 ? true : false; }};
阅读全文
0 0
- leetcode 301. Remove Invalid Parentheses BFS遍历得到所有合法的括号字符串
- LeetCode 301. Remove Invalid Parentheses(删除无效的括号)
- 【Leetcode】301. Remove Invalid Parentheses 移除非法小括号
- leetcode -- Remove Invalid Parentheses--又是DFS,BFS的题目
- leetcode:bfs:Remove Invalid Parentheses(301)
- 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
- 基于AWS习练深度学习时的10个Linux命令
- Photoshop之快速蒙版Q
- 老码农眼中的简明AI
- 从构造函数看线程安全
- Spring StringRedisTemplate 配置
- leetcode 301. Remove Invalid Parentheses BFS遍历得到所有合法的括号字符串
- 老司机教你如何优雅地完成一个小项目测试
- MySQL日期时间函数大全
- Valid Palindrome:验证是否为回文结构
- Upload
- 从啤酒和尿布讲关联规则,大数据集处理算法Apriori以及改进的PCY算法
- erlang中文编码
- 文章标题
- js其他类型转字符串