并没有看起来那么简单leetcode Generate Parentheses
问题解法参考 它给出了这个问题的探讨。
超时的代码:
这个当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的更多相关文章
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
- LeetCode Generate Parentheses 构造括号串(DFS简单题)
题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode Generate Parentheses (DFS)
题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode——Generate Parentheses
Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...
- LeetCode: Generate Parentheses [021]
[称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- leetcode Generate Parentheses python
# 解题思路:列举出所有合法的括号匹配,使用dfs.如果左括号的数量大于右括号的数量的话,就不能产生合法的括号匹配class Solution(object): def generateParenth ...
随机推荐
- hw_module_t 加载过程
每一个HAL模块都有一个ID值,以这些ID值为参数来调用硬件抽象层提供的函数hw_get_module就可以将指定的模块加载到内存来,并且获得 一个hw_module_t接口来打开相应的设备. 函数h ...
- untra edit 自动补全
选择“高级-配置-编辑器-自动完成”,勾选“自动显示‘自动完成’对话框”,并在其中定义好“当输入x个字符”时,自动补全.
- Java实现Queue类
Java实现Queue类 import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Sc ...
- HDU5468(dfs序+容斥原理)
Puzzled Elena Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- Spring Boot自定义配置与加载
Spring Boot自定义配置与加载 application.properties主要用来配置数据库连接.日志相关配置等.除了这些配置内容之外,还可以自定义一些配置项,如: my.config.ms ...
- 在struts2中配置自定义拦截器放行多个方法
源码: 自定义的拦截器类: //自定义拦截器类:LoginInterceptor ; package com.java.action.interceptor; import javax.servlet ...
- AngularJS:模块
ylbtech-AngularJS:模块 1.返回顶部 1. AngularJS 模块 模块定义了一个应用程序. 模块是应用程序中不同部分的容器. 模块是应用控制器的容器. 控制器通常属于一个模块. ...
- AngularJS:HTML DOM
ylbtech-AngularJS:HTML DOM 1.返回顶部 1. AngularJS HTML DOM AngularJS 为 HTML DOM 元素的属性提供了绑定应用数据的指令. ng-d ...
- C# winfrom FastReport Print
1.引用 using FastReport; using FastReport.Barcode; 2.code private void toolStripButtonPrint_Click(obje ...
- 一张图看懂------left join;right join;inner join