leetcode Remove Invalid Parentheses

好久没写博客了。。。蛤蛤
CSDN出了这个新的博客编辑器,有点点不习惯,嘿嘿。
我只是喜欢AC的感觉,喜欢认识问题,思考问题,想一些乱七八糟的思路,算法,然后各种debug(虽然这部分有时让我很苦恼)但是到了真真看到Accept的那一刻。全身毛孔都张开了好嘛!!!so excited!真是比连胜100把dota都要开心的感觉。
好了,废话不多说。如题。
Remove Invalid Parentheses My Submissions Question
Total Accepted: 571 Total Submissions: 1981 Difficulty: Medium
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())()”]
“)(” -> [“”]

大意就是删减掉尽量少的括号,使其字符串括号匹配。
这个题目,从一大早断断续续的想法喷涌而来,但是又有点后怕的感觉,一直畏畏缩缩的没有流畅的敲下去,上课下课吃饭看电影打dota,晚上还是回到coding中。顺着自己当初的想法一步步实现。
首先用一个DFS深搜路径,用a表示已经走过的路括号匹配状况,b表示尚未走过路径括号的状况。正则表示左括号(多几个,负责表示右括号)多几个,然后用string g表示现在位置的状态,直到路径走完位置,进行判断a是否为0(也就是说每个括号是否都有与之匹配)如是还未完成,还要进行map判断,是否出现过,然后还要记录是否删掉了最小括号,这个我写的肯定是第一次找到的是最小的,蚁后的再跟他比较就好了。然后发现还是TLE,发现这算法还可以优化剪枝,就是当a非常大,大到后面全是右括号也不能弥补为0的时候,后面的路就不必瞎鸡巴走了。就直接return好了。就这样加了一个优化条件,再改掉一些小bug,然后居然就AC了。好开心好开心。为人做的事不就是让自己快乐吗?真是让我想到昨天的一些问题,对自己认识更深了一点,嗯,开心就好。☺

#include<limits>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define LL __int64
#define eps 1e-8
#define pi acos(-1)
#define delta 0.98 //Ä£ÄâÍË»ðµÝÔö±äÁ¿
#define INF 0x7fffffff
using namespace std;
class Solution {
public:
    int l;
    vector<string>ans;
    map<string,int>mp;
    int o=0;
    void DFS(int a,int b,int p,string g,string s){
        if (fabs(a)>l-p) return;
        if (p==l){
            if (a==0){
                if ((o==0 || g.length()==o)&&mp[g]==0){
                    ans.push_back(g);
                    mp[g]=1;
                    o=g.length();
                }
            }
            return ;
        }
        if (s[p]!='(' && s[p]!=')')
            DFS(a,b,p+1,g+s[p],s);
        else
        if (s[p]==')'){
            if (a==0)   
                DFS(a,b+1,p+1,g,s);
            else{
                DFS(a-1,b+1,p+1,g+')',s);
                DFS(a,b+1,p+1,g,s);
            }
        }
        else{
            DFS(a+1,b-1,p+1,g+'(',s);
            DFS(a,b-1,p+1,g,s);
        }
        return ;
    }
    vector<string> removeInvalidParentheses(string s) {
        l=s.length();
        int k=0;
        for (int i=0;i<l;i++){
            if (s[i]=='(') k++;
            else if (s[i]==')') k--;
        }
        DFS(0,k,0,"",s);  //Ç°ÃæֵΪ0£¬ºóÃæֵΪk£¬µ½Á˵Ú0¸öλÖà 
        return ans;
    }
}gg;
int main(){
    string q="e)k()(())";
    vector<string>hh;
    hh=gg.removeInvalidParentheses(q);
    for (int i=0;i<hh.size();i++)
        cout<<hh[i]<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值