[LeetCode] Remove Invalid Parentheses

简介: This problem can be solved very elegantly using BFS, as in this post. The code is rewritten below in C++.

This problem can be solved very elegantly using BFS, as in this post.

The code is rewritten below in C++.

 1 class Solution {
 2 public:
 3     vector<string> removeInvalidParentheses(string s) {
 4         vector<string> parens;
 5         queue<string> candidates;
 6         unordered_set<string> visited;
 7         candidates.push(s);
 8         visited.insert(s);
 9         bool found = false;
10         while (!candidates.empty()) {
11             string now = candidates.front();
12             candidates.pop();
13             if (isValid(now)) {
14                 parens.push_back(now);
15                 found = true;
16             }
17             if (!found) {
18                 int n = now.length();
19                 for (int i = 0; i < n; i++) {
20                     if (now[i] == '(' || now[i] == ')') {
21                         string next = now.substr(0, i) + now.substr(i + 1);
22                         if (visited.find(next) == visited.end()) {
23                             candidates.push(next);
24                             visited.insert(next);
25                         }
26                     }
27                 }
28             }
29         }
30         return parens;
31     }
32 private:
33     bool isValid(string& s) {
34         int c = 0, n = s.length();
35         for (int i = 0; i < n; i++) {
36             if (s[i] == '(') c++;
37             if (s[i] == ')' && !c--) return false;
38         }
39         return !c;
40     }
41 };

 

目录
相关文章
LeetCode 301. Remove Invalid Parentheses
删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。 说明: 输入可能包含了除 ( 和 ) 以外的字符。
67 0
LeetCode 301. Remove Invalid Parentheses
LeetCode 241. Different Ways to Add Parentheses
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。
76 0
LeetCode 241. Different Ways to Add Parentheses
Leetcode-Easy 20. Valid Parentheses
Leetcode-Easy 20. Valid Parentheses
105 0
Leetcode-Easy 20. Valid Parentheses
LeetCode 20:有效的括号 Valid Parentheses
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 有效字符串需满足: 左括号必须用相同类型的右括号闭合。
760 0
|
Java
[LeetCode] Valid Parentheses 验证括号是否有效闭合
链接:https://leetcode.com/problems/valid-parentheses/#/description难度:Easy题目:20.
1007 0
LeetCode - 32. Longest Valid Parentheses
32. Longest Valid Parentheses  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个由'('和')'组成的字符串,求最长连续匹配子串长度.
969 0
LeetCode - 20. Valid Parentheses
20. Valid Parentheses  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个括号序列,检查括号是否按顺序匹配.
872 0
LeetCode - 22. Generate Parentheses
22. Generate Parentheses Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数n,输出由2*n个'('和')'组成的字符串,该字符串符合括号匹配规则.
861 0