[LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
LeetCode上的原题,请参见我之前的博客Generate Parentheses。
解法一:
class Solution {
public:
/**
* @param n n pairs
* @return All combinations of well-formed parentheses
*/
vector<string> generateParenthesis(int n) {
if (n == ) return {};
vector<string> res;
helper(n, n, "", res);
return res;
}
void helper(int left, int right, string out, vector<string>& res) {
if (left < || right < || left > right) return;
if (left == && right == ) {
res.push_back(out);
return;
}
helper(left - , right, out + "(", res);
helper(left, right - , out + ")", res);
}
};
解法二:
class Solution {
public:
/**
* @param n n pairs
* @return All combinations of well-formed parentheses
*/
vector<string> generateParenthesis(int n) {
set<string> res;
if (n == ) {
res.insert("");
} else {
vector<string> pre = generateParenthesis(n - );
for (auto a : pre) {
for (int i = ; i < a.size(); ++i) {
if (a[i] == '(') {
a.insert(a.begin() + i + , '(');
a.insert(a.begin() + i + , ')');
res.insert(a);
a.erase(a.begin() + i + , a.begin() + i + );
}
}
res.insert("()" + a);
}
}
return vector<string>(res.begin(), res.end());
}
};
[LintCode] Generate Parentheses 生成括号的更多相关文章
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- generate parentheses(生成括号)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 022 Generate Parentheses 生成括号
给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[ "((()))", "(()())", "(())() ...
- 22.Generate Parentheses[M]括号生成
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode OJ:Generate Parentheses(括号生成)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 22. Generate Parentheses生成指定个括号
生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ...
随机推荐
- WIN7系统下U盘安装Ubuntu双系统
1. 准备工作 1. 官网下载Ubantu镜像,我下载的是Ubantu14.04.3 LTS版本: 2. 下载 Universal USB Installer (UUI)用于制作U盘启动盘: 3. 下 ...
- Waiting Processed Cancelable ShowDialog (Release 2)
namespace Test { using System; using System.Windows.Forms; static class Program { /// <summary> ...
- C语言文件操作fclose在NDK引起的BUG
今天在NDK中写了一个简单的写入文件操作: FILE *fp = fopen("/sdcard/test.txt","w"); if(fp == NULL) { ...
- (转载)一个用于Gnome桌面的下拉式终端: Guake 0.7.0 发布
转自:https://linux.cn/article-5507-1.html Linux的命令行是最好.最强大的东西,它使新手着迷,并为老手和极客的提供极其强大的功能.那些在服务器和生产环境下工作的 ...
- 【框架】网络请求+Gson解析--Retrofit 2
其实内部是封装了Okhttp和Gson解析 public class CourseFragmentAPI { public static void get(String userId, BaseCal ...
- 把textarea右下角的灰点去掉
这个是浏览器自带功能,那个区域属于滚动条的占位区 使textarea可以调整size,加 style="resize:none"可解决,添加多行文字时还是会出现滚动条的.
- Android学习系列(41)--Android Studio简单使用
1. 环境 UBUNTU 14.04 + Android Studio 0.8.2 2. 安装jdk openjdk-7是一个很好的选择: sudo apt-get update sudo apt-g ...
- hdu1248完全背包
不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前.死亡骑士:"我要买道具!" ...
- express-21 静态内容
静态内容是指应用程序不会基于每个请求而去改变的资源. 多媒体: 图片.视频和音频文件 CSS: JavaScript 二进制下载文件: 这包含所有种类:PDF.压缩文件.安装文件等类似的东西. 借助一 ...
- 设置随机启动--《用delphi开发共享软件》-15.1任务管理器
在设置窗体中 chkAutoStart: TCheckBox; 在设置窗体中 chkAutoStart: TCheckBox; procedure TFrmSetup.FormCreate(Sende ...