Generate Parentheses
Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] 很有趣的递归方法,由于字符串只有左括号和右括号两种字符,而且最终结果必定是左括号3个,右括号3个,所以我们定义两个变量left和right分别表示剩余左右括号的个数,如果在某次递归时,
左括号的个数大于右括号的个数,说明此时生成的字符串中右括号的个数大于左括号的个数,即会出现')('这样的非法串,所以这种情况直接返回,不继续处理。如果left和right都为0,则说明此时生成的字符串已有3个左括号和3个右括号,
且字符串合法,则存入结果中后返回。如果以上两种情况都不满足,若此时left大于0,则调用递归函数,注意参数的更新,若right大于0,则调用递归函数,同样要更新参数。代码如下:
class Solution {
public:
static void process(int l, int r, string item, vector<string>& res){
if (r < l)
return;
if (l == r && l == 0)
res.push_back(item);
if (l>0)
process(l - 1, r, item + '(', res);
if (r > 0)
process(l, r-1, item + ')', res);
}
static vector<string> generateParenthesis(int n) {
vector<string> res;
if (n == 0)
return res;
process(n, n, "", res);
return res;
}
};
Generate Parentheses的更多相关文章
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode022. Generate Parentheses
leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- leetcode-algorithms-22 Generate Parentheses
leetcode-algorithms-22 Generate Parentheses Given n pairs of parentheses, write a function to genera ...
随机推荐
- DB2导入导出数据库数据
导出数据库中数据 在db2cmd命令下生成建库脚本(-z指定模式名) db2look -d BBS -z db2admin -u db2admin -e -o bbs.sql 在db2cmd命令下导出 ...
- 学习 jsonp
1.起因 js脚本做ajax异步调用的时候,直接请求普通文件存在跨域无权限访问的问题,不管你是静态页面.动态网页.web服务,只要是跨域请求,都无法成功: 如果上句话没明白,我们直接看例子.有两个一模 ...
- ORA-00030: User session ID does not exist.
同事在Toad里面执行SQL语句时,突然无线网络中断了,让我检查一下具体情况,如下所示(有些信息,用xxx替换,因为是在处理那些历史归档数据,使用的一个特殊用户,所以可以用下面SQL找到对应的会话信息 ...
- 从Prototype学习JavaScript面向对象编程
概述 JavaScript是一种基于对象的编程语言.它是灵活的,既有面向过程(也就是面向函数)的编程,也有面向对象的编程.因此我称它是基于对象的编程语言. 对于JavaScript的面向过程的编程特性 ...
- C# 多线程,论多核时代爱恨情仇
为什么要学习多线程? 2010年1月21日是10年某市公务员考试的报名截止日.因从下午2点开始,用于报名的北京市人事考试网瘫痪,原定于昨天下午5点截止的报名时间延迟至今天上午11点. 2011年3月1 ...
- C# 读取在存储过程多结果集
--SQL Server 测试环境搭建: Create database Test; go USE [Test] GO if OBJECT_ID('Tab','U') is not null drop ...
- centos7安装CDH5.5.0
1.安装jdk mkdir -p /usr/java tar zxvf jdk-7u80-linux-x64.tar.gz -C /usr/java/ vi /etc/profile export J ...
- 【mysql】高可用集群之MMM
一.复制的常用拓扑结构 复制的体系结构有以下一些基本原则: (1) 每个slave只能有一个master: (2) 每个slave只能有一个唯一的服务器ID: (3) 每个maste ...
- cocosstdio之字体之文本和FNT字体
FNT字体和文本字体的作用是:导入字体资源可以使用字体资源便可以使用其资源内的字体来在程序中使用 不同的是FNT字体资源内容比较少,所以个人猜想可以在特定情况下使用: 两种字体资源对比: 赋值过程对比 ...
- Redis安装和配置
1.下载安装redis 在linux服务器上,命令行执行以下命令(cd ./usr local/src 一般源码放在这里(推荐源码安装)) wget http://download.redis.io/ ...