问题解法参考 它给出了这个问题的探讨。

超时的代码:

这个当n等于7时,已经要很长时间出结果了。这个算法的复杂度是O(n^2)。

#include<iostream>
#include<vector>
#include<stack>
#include<map> using namespace std; bool isValid(string s) {
map<char, char> smap;
smap.insert(make_pair('(', ')'));
stack<char> ss;
int i = ;
int L = s.length();
while (i<L)
{
char c = s[i];
if (!ss.empty() && c == smap[ss.top()])
{
ss.pop();
}
else
{
ss.push(c);
}
i++;
}
if (ss.empty())
{
return true;
}
else
return false;
} vector<string> F(int n, string s, vector<string> &result, vector<int> a)
{
if (n < )
{
return result;
}
if (n == )
{
for (int i = ; i < a.size(); i++)
{
string t = s;
t += a[i];
if (isValid(t))
result.push_back(t);
}
}
else
{
for (int i = ; i < a.size(); i++)
{
string t = s;
t += a[i];
F(n - , t, result, a);
}
}
return result;
} vector<string> generateParenthesis(int n) {
vector<int> a = {'(',')'};
vector<string> result;
string s = "";
return F(n*, s, result, a);
} int main()
{
vector<string> result = generateParenthesis();
for (int i = ; i < result.size(); i++)
{
cout << result[i].c_str() << endl;
}
return ;
}

上面的解法显然代价是O(2^n)这个肯定超时。不知道自己当时写的时候就没有分析一下,加上今天写hiho coder上的那一题,由此可见对内存的开销和时间的代价还是不够敏感!

这个题分析一下还是简单的,首先就两中符号,不是这个就是另一个,所以只要满足当前左括号的数目大于右括号就可以加在字符串后添加右括号,如果左括号没有放完就是左括号的数目还没有到n,那么就可以继续放左括号。但是但是但是但是但是……哎,我竟然又犯了一个错误。

递归是递归,循环是循环不要混。 递归时一定要保证一次循环加一个。

我刚开始想的是当左括号大于右括号是可以这样写

if (left > right){
unguarded_generate(n, left + 1, right, result, s + ')');
unguarded_generate(n, left, right + 1, result, s + ')');
}

too young too simple啊!

AC代码:

 #include<iostream>
#include<vector> using namespace std; void unguarded_generate(int n, int left, int right, vector<string> &result, string s){
if (left == n&&right == n){
result.push_back(s);
}
else{
if (left != n){
unguarded_generate(n, left + , right, result, s + '(');
}
if (left > right&&right != n){
unguarded_generate(n, left, right + ,result, s + ')');
}
}
} vector<string> generateParenthesis(int n)
{
vector<string> ret;
unguarded_generate(n, , , ret, "");
return ret;
} int main(){
vector<string> result = generateParenthesis();
for (int i = ; i < result.size(); i++){
cout << result[i].c_str() << endl;
}
}

并没有看起来那么简单leetcode Generate Parentheses的更多相关文章

  1. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  2. LeetCode: Generate Parentheses 解题报告

    Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...

  3. [LeetCode]Generate Parentheses题解

    Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...

  4. LeetCode Generate Parentheses 构造括号串(DFS简单题)

    题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...

  5. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. LeetCode Generate Parentheses (DFS)

    题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  7. LeetCode——Generate Parentheses

    Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...

  8. LeetCode: Generate Parentheses [021]

    [称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  9. leetcode Generate Parentheses python

    # 解题思路:列举出所有合法的括号匹配,使用dfs.如果左括号的数量大于右括号的数量的话,就不能产生合法的括号匹配class Solution(object): def generateParenth ...

随机推荐

  1. LeetCode 323. Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  2. [ Laravel 5.5 文档 ] 数据库操作 —— 在 Laravel 中轻松实现分页功能

     简介 在其他框架中,分页是件非常痛苦的事,Laravel 让这件事变得简单易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基于 ...

  3. JDBC小常识

    1 JDBC连接数据库6步 Load the JDBC Driver Establish the Database Connection Create a Statement Object Execu ...

  4. WPF中ToolTip的自定义

    ToolTip或者PopUp这个控件在做界面时会经常用到.如何对ToolTip进行自定义呢? 1.首先自定义tooltip的controlTemplate,完全清除系统默认效果, 如下:        ...

  5. unix下网络编程之I/O复用(四)

    首先需要了解的是select函数: select函数 #include<sys/select.h> #include<sys/time.h> int select (int m ...

  6. maven打包指定main函数的入口,生成依赖的包

    为了使Jar包中指定Main方法位置和生成依赖包,需要在pom文件中加入如下配置: <build> <plugins> <plugin> <groupId&g ...

  7. 机器学习:衡量线性回归法的指标(MSE、RMSE、MAE、R Squared)

    一.MSE.RMSE.MAE 思路:测试数据集中的点,距离模型的平均距离越小,该模型越精确 # 注:使用平均距离,而不是所有测试样本的距离和,因为距离和受样本数量的影响 1)公式: MSE:均方误差 ...

  8. C语言生成程序问题

    问题: 我用VS2013写好C语言程序调试运行后就在debug文件夹下生成了EXE文件,可以在本机运行.但是这个EXE文件在别的没装过VS2013的电脑上就不能直接运行,说丢失MSVCR120D.dl ...

  9. DAY20-Django之FileField与ImageField

    ImageField 和 FileField 可以分别对图片和文件进行上传到指定的文件夹中. 1. 在下面的 models.py 中 : picture = models.ImageField(upl ...

  10. maven依赖scope配置项讲解

    我们在使用Maven配置依赖项的时候,常常只会配置Maven的坐标以及版本信息就可以了,但我们看其他人的工程代码的时候常常会见到有个scope配置项,今天就来分别介绍下这个配置下几个类别的作用. &l ...