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

超时的代码:

这个当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. 难道调用ThreadPool.QueueUserWorkItem()的时候,真是必须调用Thread.Sleep(N)吗?

    开门见山,下面的例子中通过调用ThreadPool.QueueUserWorkItem(WaitCallback callBack, object state)的方式实现异步调用: 1: class ...

  2. bzoj 4300 绝世好题——DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4300 考虑 dp[ i ] 能从哪些 j 转移过来,就是那些 a[ j ] & a[ ...

  3. Python collections系列之默认字典

    默认字典(defaultdict)  defaultdict是对字典的类型的补充,它默认给字典的值设置了一个类型. 1.创建默认字典 import collections dic = collecti ...

  4. Makefile 自动产生依赖 ***

    代码如下: 其实这里主要是为每个C文件建立一个同名的后缀为.d.该文件的作用是使用gcc的-M属性来自动生成.o文件的头文件依赖关系. 第1,2,4都好理解. 第2行解释: 使用gcc -M 的属性将 ...

  5. 推荐几个MySQL大牛的博客

    1.淘宝丁奇 http://dinglin.iteye.com/ 2.周振兴@淘宝 花名:苏普 http://www.orczhou.com/ 3. 阿里云数据库高级专家彭立勋为 MariaDB Fo ...

  6. NET代码运行在服务器JS运行在客户端

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;usi ...

  7. [置顶] linux getline()函数

    getline()函数是什么?百度百科这样解释:      getline不是C库函数,而是C++库函数.它会生成一个包含一串从输入流读入的字符的字符串,直到以下情况发生会导致生成的此字符串结束.1) ...

  8. Java事务的原理与应用

    Java事务的原理与应用 一.什么是Java事务 事务必须服从ISO/IEC所制定的ACID原则.ACID是原子性(atomicity).一致性(consistency).隔离性 (isolation ...

  9. 2014.2.23加载大数据时不闪烁的listView

    namespace 相册处理 { //将ListView重载为此新类,解决加载过程中闪烁的问题 //在designer.cs中改写: //private ListViewNeverFlickering ...

  10. spring bean管理

    轻量级,无侵入 Bean管理 1 创建applicationContext.xml 2 配置被管理的Bean 3 获取Bean pom.xml配置 <dependency> <gro ...