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())()"]
")(" -> [""]

Credits:
Special thanks to @hpplayer for adding this problem and creating all test cases.

思路:BFS。首先将s压入队列,循环取出队列头部的字符串,如果其合法,将其加入返回的数组ret中,并设置bool常量found为true。因为我们返回的是括号数目最多的合法字符串,如果found为true,表明找到了一个合法的字符串,后面的字符串没有必要进行切割处理了,所以found为true,直接continue跳过循环。否则尝试切割字符串中的一个左右括号,将其压入队列q中,后续判断其是否合法。注意,为了保证字符串不出现重复,这里用了unordered_map判断是否出现重复字符串,只有首次出现的字符串才会进入队列q中。

  1. class Solution {
  2. private:
  3. bool isValid(string s){
  4. int count=0;
  5. for(int i=0;i<s.length();i++){
  6. if(s[i]==‘(‘)
  7. count++;
  8. else if(s[i]==‘)‘){
  9. if(count==0)
  10. return false;
  11. count--;
  12. }
  13. }
  14. return count==0;
  15. }
  16. vector<string> ret;
  17. public:
  18. vector<string> removeInvalidParentheses(string s) {
  19. unordered_map<string,int> map;
  20. queue<string> q;
  21. q.push(s);
  22. map[s]=1;
  23. bool found=false;
  24. while(!q.empty()){
  25. string str=q.front();
  26. q.pop();
  27. if(isValid(str)){
  28. ret.push_back(str);
  29. found=true;
  30. }
  31. if(found)
  32. continue;
  33. for(int i=0;i<str.length();i++){
  34. if(str[i]!=‘)‘&&str[i]!=‘(‘)
  35. continue;
  36. //将这个括号从字符串中去除
  37. string sub=str.substr(0,i)+str.substr(i+1);
  38. if(map.find(sub)==map.end()){
  39. q.push(sub);
  40. map[sub]=1;
  41. }
  42. }
  43. }
  44. return ret;
  45. }
  46. };

来自为知笔记(Wiz)

时间: 2024-11-04 19:01:23

301.Remove Invalid Parentheses的相关文章

[leetcode] 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: "()())()" -> ["()()()",

[LeetCode] 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:"()())()" -> ["()()()", &

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 ). Example 1: Input: "()())()" Output: [&quo

LeetCode题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 ). Example 1: Input: "()())()" Output: ["()()

[leetcode]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 ). Example 1: Input: "()())()"Output: ["()()(

LeetCode 301. Remove Invalid Parentheses(DP)

题目 DP 险过. dp[i][j] :means it need remove at least dp[i][j] characters to get vaild parenthese from position i to postion j in string. vector str[i][j] store the parenthese string for example : "()())" dp[0][1]=0 vector[0][1]=["()"] dp[

【leetcode】301. Remove Invalid Parentheses

题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示这个区间是不合法的,需要删掉一个右括号:遍历完成后,如果左括号数量大于右括号的数量,那么需要删除左括号,直至两者相等. 代码如下: class Solution(object): def removeInvalidParentheses(self, s): """ :type s

leetcode Remove Invalid Parentheses

题目连接 https://leetcode.com/problems/remove-invalid-parentheses/ Remove Invalid Parentheses Description Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may co

[LeetCode][JavaScript]Remove Invalid Parentheses

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: "()())()"