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

超时的代码:

这个当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. [转载]python datetime处理时间

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  2. HTML 5中的结构元素

    1.header:标记头部区域的内容 .footer:标记页脚区域的内容 .section:Web页面中的一块区域 4.article:独立的文章内容区域 5.aside:相关侧边内容或者引文区域 6 ...

  3. &(((struct A*)NULL)->m_float)---offsetof

    问题描述: struct A { int m_int; float m_float; }; int main(void) { printf("%p",&(((struct ...

  4. 简单的触发黑名单阻断演示 control+c

    #include "stdafx.h"#include <signal.h>#include <windows.h> #include <iostre ...

  5. 为什么在进行Full GC之前最好进行一次Minor GC

    摘自:<Java Performance>第三章 为什么在进行Full GC之前最好进行一次Minor GC? Garbage collecting the young generatio ...

  6. 问题:不支持Dictionary;结果:在Web Service中傳送Dictionary

    在Web Service中傳送Dictionary 有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMe ...

  7. javascript删除option选项的多种方法总结

    转自:https://blog.csdn.net/xiaoxuanyunmeng/article/details/16886505 1. JavaScript 代码如下: var oSel=docum ...

  8. 需要network lightweight filter disk 上的文件netft.sys

    小米wifi在win10下安装不成功,需要network lightweight filter disk 上的文件ntflt.sys 默认路径有问题,改成下面的路径好了! 选择下面第一个路径安装成功了 ...

  9. Linux 查看一个端口的连接数

    netstat -antp|grep -i "80" |wc -l 譬如查看80端口的连接数

  10. 虚拟机VMware的安装以及指南

    VMware是一个非常强大的虚拟软件,它的更新速度非常的快,随着软件的更新速度的加快,它的大小会越来越大,但是新的版本大多数会是给企业使用的,对于我们而言,不那么的需要,所以,我们只需要使用一些差不多 ...