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

超时的代码:

这个当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. AtCoder Petrozavodsk Contest 001 A - Two Integers

    Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement You are given positive ...

  2. 使用swing构建一个界面(包含flow ,Border,Grid,card ,scroll布局)

    package UI; import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.Cursor;import ja ...

  3. What makes an inferred latch? how To avoid creating inferred latches? when do you know you need latches?

    What makes an inferred latch?For combinatorial logic, the output of the circuit is a function of inp ...

  4. POJ3264(RMQ-ST算法)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 47087   Accepted: 22101 ...

  5. 【转】 Pro Android学习笔记(八二):了解Package(1):包和进程

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在之前,我们已经学习了如何签发apk,见P ...

  6. 四川第七届 I Travel(bfs)

    Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Amo ...

  7. docker 端口映射iptables: No chain/target/match by that name错误解决方法

    pkill docker iptables -t nat -F ifconfig docker0 down brctl delbr docker0 service docker restart

  8. 很详细的Nginx配置说明

    这篇文章主要为大家分享了一篇很详细的Nginx配置说明,主要内容包括Nginx常用功能.Nginx配置文件结构,想要了解Nginx配置的朋友不要错过,参考一下   Nginx是lgor Sysoev为 ...

  9. DAY5-模块与包

    一.模块的介绍 1.什么是模块 #常见的场景:一个模块就是一个包含了一组功能的python文件,比如spam.py,模块名为spam,可以通过import spam使用. #在python中,模块的使 ...

  10. spring-boot 热加载实现替换Jrebel

    导读: 本文主要说说,在玩spring-boot时,我们经常要遇到重启服务这种浪费时间的事情,为了割掉这个痛点,我们一般有2中方式实现. 一个是springload , 另外一个是 spring-bo ...