【Leetcode】之Generate Parentheses
来源:互联网 发布:mac病毒查杀 编辑:程序博客网 时间:2024/05/01 07:11
一.问题描述
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
“((()))”, “(()())”, “(())()”, “()(())”, “()()()”
二.我的解题思路
这道题目需要寻找所有满足规范的括号组合,我的思路是采用回溯法,解空间是排列树,并且使用堆栈处理作为限界函数。程序测试结果没啥问题,但是最后提交的时候超时了,感觉这样做的时间复杂度也没有多高。。。
class Solution {public: void swap(char & a,char &b){ char tmp; tmp=a; a=b; b=tmp; } void back_track(int t,int m,string & res,vector<string>& ans,map<string,int>&hash_map,stack<char>& curr){ if(t>=m){ if(hash_map[res]!=1){ ans.push_back(res); hash_map[res]=1; } } for(int j=t;j<m;j++){ swap(res[j],res[t]); if(res[t]=='('){ curr.push(res[t]); back_track(t+1,m,res,ans,hash_map,curr); } else{ if(!curr.empty()){ char ttmp = curr.top(); if(ttmp=='('){ curr.pop(); back_track(t+1,m,res,ans,hash_map,curr); } } } swap(res[j],res[t]); } } vector<string> generateParenthesis(int n) { string tmp(2*n,'('); map<string,int> hash_map; stack<char> curr; for(int i=0;i<2*n;i=i+2){ tmp[i] = ')'; tmp[i+1]='('; } string res;res=tmp; //string res("(())"); vector<string> ans; int i=0;int m=2*n; back_track(i,m,res,ans,hash_map,curr); return ans; }};
查看了一下别人的实现思路,是用递归的思想实现的:
class Solution {public: vector<string> generateParenthesis(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function rec=vector<vector<string> >(n+1); vector<string> ret=generate(n); return ret; } vector<vector<string> > rec; vector<string> generate(int n) { if (!rec[n].empty()) return rec[n]; vector<string> ret; if ( n==0) { ret.push_back(string("")); return ret; } if ( n==1) { ret.push_back(string("()")); return ret; } vector<string> in; vector<string> out; int i,j,k; string tmp; for( k=n-1;k>=0;k--) { in=generate(k); out=generate(n-1-k); for(i=0;i<in.size();i++) { for(j=0;j<out.size();j++) { tmp.clear(); tmp.append(string("(")); tmp.append(in[i]); tmp.append(string(")")); tmp.append(out[j]); ret.push_back(tmp); } } } rec[n]=ret; return ret; }};
0 0
- leetcode之 Generate Parentheses
- leetcode之Generate Parentheses
- leetcode之 Generate Parentheses
- 【Leetcode】之Generate Parentheses
- LeetCode之22----Generate Parentheses
- leetcode之 Generate Parentheses(Catalan数)
- leetcode之路022 Generate Parentheses
- LeetCode之Generate Parentheses(C++)
- LeetCode进阶之路(Generate Parentheses)
- LeetCode: Generate Parentheses
- LeetCode: Generate Parentheses
- [Leetcode] Generate Parentheses
- leetcode 51: Generate Parentheses
- [LeetCode] Generate Parentheses
- Leetcode: Generate Parentheses
- LeetCode Generate Parentheses
- 【leetcode】Generate Parentheses
- [LeetCode]Generate Parentheses
- php apache mysql服务器搭建学习笔记
- Java注册手机获取验证码
- Android开源项目(类库+控件)
- group by 优化临时表
- jboss完全可以独立运行 servlet jsp ssh mybits spring mvc
- 【Leetcode】之Generate Parentheses
- 一次查找sqlserver死锁的经历
- 如何配置pl/sql 连接远程oracle服务器
- Oracle中清除游标缓存的几种方法
- andorid平板中安装完整的ubuntu系统
- 第四章 Controller接口控制器详解(5)——跟着开涛学SpringMVC
- CMake编译运行OpenCV程序_debug总结
- Elasticsearch 全字段搜索_all,query_string查询,不进行分词
- Weka聚类算法的两个实现算法