LeetCode 20 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:
"((()))", "(()())", "(())()", "()(())", "()()()"
还是类似的括号匹配问题,这次是要生成所有可能的匹配组合,思路如下:
首先用迭代法穷举可能出现的所有情况,然后利用已有的isValid()判断是否匹配,进而决定是否储存到返回vector容器中。
class Solution {
public:
vector<string> generateParenthesis(int n)
{
mSize = n * ;
init();
string s;
genpa(-, s);
return ret;
}
bool isValid(string s)
{
stack<char> stk;
int nSize = s.size();
for(int i = ; i!= nSize; ++i)
{
if(s[i] == '{' || s[i] == '(' || s[i]== '[')
{
stk.push(s[i]);
}
else
{
if(stk.empty())
{
return false;
}
char cElem = stk.top();
if(cElem - s[i] ==- || cElem - s[i] == -)
{
stk.pop();
continue;
}
else
{
return false;
}
}
}
if(!stk.empty())
{
return false;
}
return true;
}
void genpa(int j, string s)
{
if(j == mSize -)
{
if(isValid(s))
{
ret.push_back(s);
}
return;
}
for(int i = ; i!= ; ++i)
{
genpa(j+, s+album[i]);
}
}
void init(void)
{
album[] = '(';
album[] = ')';
}
private:
int mSize;
vector<string> ret;
char album[];
};
思路类似,多举一反三。
LeetCode 20 Generate Parentheses的更多相关文章
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- LeetCode 022 Generate Parentheses
题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- [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 ...
- 【leetcode】Generate Parentheses
题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
随机推荐
- fzu1686-神龙的难题
给出一个n\times m的01矩阵,以及\(h,w\),表示一次可以把矩阵的一个\(h\times w\)的小矩阵变为全0,问至少要多少次可以把整个矩阵变为全0.\(n,m\le 15\). 分析 ...
- Qt——设计颜色编辑选取对话框
Qt中已经有一些封装好的对话框,比如QMessageBox.QColorDialog等,使用起来快捷方便,但缺点是我们无法为它们自定义样式,所以可能难以“融入”我们的项目.既然如此,那就自己做一个把. ...
- Python面向对象—类的继承
一个子类可以继承父类的所有属性,不管是父类的数据属性还是方法. class Father(object): num = 0 def __init__(self): print "I'm Pa ...
- CentOS 文件特殊权限SUID,SGID,SBIT
1.SUID ,是一种对二进制程序进行设置的特殊权限,可以让二进制程序的执行者临时拥有所有者的权限(仅对拥有执行权限的二进制程序有效). (1)SUID权限仅对二进制程序有效: (2)本权限仅在执行该 ...
- [JSOI2007]重要的城市 floyd:最短路计数
---题面--- 题解: 其实感觉还是比较妙的,第一眼看题想到floyd统计最短路条数, 注意到对于任意两点x,y而言,floyd将会枚举其最短路所可能经过的所有中转点, 因此我们可以直接分别统计对于 ...
- MySQL - General error: 1390 Prepared statement contains too many placeholders
报错原因:预处理 SQL语句时使用的占位符数量超过了最大限制(默认65535). 解决方案:拆分查询语句,每次使用的占位符低于限制即可.
- Mac安装mysqldb
一. 安装mysql (一)下载地址 https://pan.baidu.com/s/1slw50LZ 安装成功后,在系统偏好设置里有MySQL图标,可以启动或关闭MySQL 二. Mysql roo ...
- AOJ.859 地毯填补问题 (递归与分治)
AOJ.859 地毯填补问题 (递归与分治) 题意分析 学习分治思想,第一次接触, 代码总览 #include <iostream> #include <cstdio> #in ...
- Vue_WebPack小白入门
Vue语法笔记 Vue项目搭建过程 Vue问题总结 去掉vue 中的代码规范检测(Eslint验证) 解决跨域请求问题 Vue推荐资料
- git安装配置和使用
## 安装git服务器 ## 安装git sudo apt-get install git ## 建立git用户 sudo adduser git ## 修改git用户 * 设置不能登录 vim /e ...