LeetCode22 Generate Parentheses
题意:
iven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is: (Medium)
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
分析:
生成满足数量要求的所以可能的匹配的括号。实际就是一道回溯或者深搜题。
当左括号还没有达到数量的时候,可以添加左括号;当右括号比左括号数量少的时候可以添加右括号;
搜索所有情况即可。有了昨天的17题练习过之后,这个回溯写的就比较顺手了。
代码:
class Solution {
void dfs(vector<string>& v, string& s, int n,int left) {
if (s.size() == n) {
v.push_back(s);
return;
}
int right = s.size() - left;
if (left < n / ) {
s.push_back('(');
dfs(v,s,n,left+);
s.pop_back();
}
if (right < left) {
s.push_back(')');
dfs(v,s,n,left);
s.pop_back();
}
return;
}
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
if (n == ) {
return result;
}
string s;
dfs(result,s,*n,);
return result;
}
};
LeetCode22 Generate Parentheses的更多相关文章
- Leetcode22. Generate Parentheses(生成有效的括号组合)
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:
- Leetcode22.Generate Parentheses括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- [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< ...
随机推荐
- opengpg
- CF160D
题意:给你一个图,判断每条边是否在最小生成树MST上,不在输出none,如果在所有MST上就输出any,在某些MST上输出at least one: 分析:首先必须知道在最小生成树上的边的权值一定是等 ...
- poj 1564 Sum It Up (DFS+ 去重+排序)
http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...
- Linux top和负载的解释
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法. top - 01:06:48 up 1:22, ...
- 如何让ASP.NET网站站点不停止 永远持续运行
转载自 http://www.cnblogs.com/insus/p/3658752.html 在公司内部服务器,运行很多网站(应用程序),但每个网站都有自动化或是定时执行的事务.后来经整合,所有这 ...
- django admin site配置(二)
1. ModelAdmin.inlines 将有外键的子类包含进视图 ,实例: class Author(models.Model): name = models.CharField(max_leng ...
- 数据库连接池 c3p0 demo 代码和分析
import java.sql.Connection; import java.sql.SQLException; import java.beans.PropertyVetoException; i ...
- Linux 上的基础网络设备详解
抽象网络设备的原理及使用 网络虚拟化是 Cloud 中的一个重要部分.作为基础知识,本文详细讲述 Linux 抽象出来的各种网络设备的原理.用法.数据流向.您通过此文,能够知道如何使用 Linux 的 ...
- TL-WR703 USB不稳定/当前的总结
http://see.sl088.com/wiki/WR703_USB%E4%B8%8D%E7%A8%B3%E5%AE%9A/%E5%BD%93%E5%89%8D%E7%9A%84%E6%80%BB% ...
- uva10327 - Flip Sort
Flip Sort Sorting in computer science is an important part. Almost every problem can be solved effec ...