leetcode Remove Invalid Parentheses
2015-11-05 22:41
302 查看
好久没写博客了。。。蛤蛤
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了。好开心好开心。为人做的事不就是让自己快乐吗?真是让我想到昨天的一些问题,对自己认识更深了一点,嗯,开心就好。☺
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; }
相关文章推荐
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- Hadoop2.6+jdk8的安装部署(1)——使用jar包安装部署【详细】
- Hadoop FS Shell
- DFS使用方法总结
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem