艾恩凝
个人博客 https://aeneag.xyz/
公众号 技术乱舞
今天的题自己感觉有点难
2021/10/15
题目
https://leetcode-cn.com/problems/remove-invalid-parentheses/
题目分析
又是作废的一天,BFS,好想法,参考网上大佬的,为什么这么菜
删除字符串的一个节点,看看是否可以
题解
class Solution {
public:
bool Is_Valid(const string& s){
int count = 0;
for( char c : s){
if(c == '(')++count;
if(c == ')')--count;
if(count < 0)return false;
}
return count == 0 ;
}
vector<string> removeInvalidParentheses(string s) {
unordered_set<string> word{s};
while(true){
unordered_set<string> res;
for(auto& w : word){
if(Is_Valid(w))
res.insert(w);
}
if(!res.empty())
return vector<string>(res.begin(),res.end());
unordered_set<string> next_word;
for (const string& t : word) {
for (int i = 0; i < t.length(); ++i) {
if (t[i] == '(' || t[i] == ')') {
next_word.insert(t.substr(0, i) + t.substr(i+1,t.size()-i-1));
}
}
}
word = next_word;
}
return {};
}
};
欢迎关注 #公众号:技术乱舞 一起交流
灵魂碰撞