leetcode题解 Generate Parentheses
原文链接:https://leetcode.com/problems/generate-parentheses
给出数字n,求n对括号组成的合法字符串。
刚做出来,敲完代码,修改了一次,然后提交,accepted ~ 还是小有成就感的。。菜鸟...
先记录下自己的笨方法。再研究更高妙的方法。
刚看到题也是一头雾水。没法深入思考,感觉就是不断的罗列,被题目吓到了。
仔细观察,合法字符串的第一个字母肯定是( ,最后一个肯定是)。
对于合法字符串的每一位,必定有 左括号的个数 >= 右括号的个数。每一位都必须满足。
想法就是,从第一位开始向最后一位扩充,直到填满2*n位。
当填第i位的时候,考察前面左括号的个数left,和右括号的个数right。有下面三种情况:
1、left == right 左右括号相等了,这时候只能填左括号。
2、left == n , right < n 左括号已经有n个了,满了,只能填右括号了。
3、left < n && left > right 左括号还没满,且比右括号多。这时候,可以填两种。
本来想递推算,但是后来习惯性改成了循环。
用C太麻烦了!
C++的STL,边搜语法边照着敲的。肯定用的很蹩脚。见谅。
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<int>left; //第i个字符串左括号的个数
vector<int>right; //第i个字符串右括号个数
vector<string> ans;
int i,j,k;
string tmp("(");
ans.push_back(tmp); //第一个字符串就是"("
left.push_back(); //第一个字符串的左括号有一个
right.push_back(); //右括号0。
for(i = ; i<*n; i++) //开始添加第i位。
{
int len = ans.size();
for(j = ;j < len; j++) //遍历每个字符串
{
if(left[j] == right[j])
{
ans[j].append(,'(');
left[j] ++;
}
else if(left[j]>right[j] && left[j] < n)
{
string tmp(ans[j]);
tmp.append(,'(');
ans.push_back(tmp);
left.push_back(left[j]+);
right.push_back(right[j]);
ans[j].append(,')');
right[j]++;
}
else if(left[j] == n)
{
ans[j].append(,')');
right[j]++;
}
}
}
return ans;
}
};
leetcode题解 Generate Parentheses的更多相关文章
- [LeetCode 题解]: 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 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之 Generate Parentheses
题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...
- [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 ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- folly无锁队列正确性说明
folly无锁队列是facebook开源的一个无所队列,使用的是单向链表,通过compare_exchange语句实现的多生产多消费的队列,我曾经花了比较多的时间学习memory_order的说明,对 ...
- 修改OBS为仅直播音频
准备工作: 1. VS2013 的最新更新版或者VS2015 2. QT Creater 5.7 https://www.qt.io/ 3. CMake (cmake-gui) 4. obs 依 ...
- 云端搭建内网局域网+NAT冗余上网:vps-centos6.10 +pptp client +2个ros 实现默认走pptp上网,万一pptp断了,走另外一个ros路由+centos7补充了下
介绍下环境: 1.ROS1也是PPTP SERVER,IP为172.16.22.3/24,pptp pool为172.16.23.0/24,pptp的默认帐号是111,密码是123 2.ROS2的IP ...
- Jmeter(四)NO-GUI模式运行
在前几篇中有提到NO-GUI模式的运行,是的,Jmeter支持NO-GUI方式的运行. 如果Jmeter的环境搭建完毕,那么在命令行下执行jmeter - ?便会出现jmeter的各个参数 --? p ...
- Socket的长连接和短连接
讨论Socket必讨论长连接和短连接 一.长连接和短连接的概念 1.长连接与短连接的概念:前者是整个通讯过程,客户端和服务端只用一个Socket对象,长期保持Socket的连接:后者是每次请求,都新建 ...
- Zabbix故障总结(持续更新)
Zabbix housekeeper processes more than 75% busy 问题原因 为了防止数据库持续增大,zabbix有个自动删除历史数据的机制,就是housekeeper,而 ...
- 小朋友学C语言(2):安装Dev C++编译器
(一)编译器 编译器是将“一种语言(通常为高级语言)”翻译为“另一种语言(通常为低级语言)”的程序.一个现代编译器的主要工作流程:源代码 (source code) -->预处理器 (prepr ...
- elasticsearch License 到期后更新步骤
ELK下载安装后有一个月试用期,到期后需要更新License,且每个License有效期为 1 年,License过期前10天里相关log会一直出现,提醒用户更新,支持实时更新无需重启服务. 步骤: ...
- ELK新手教程——pomelo + log4js + ELK(一)
随着项目越来越庞大,日志的数量也随之增多,游戏上线后的日志量肯定更多了,所以对日志的收集.存储.查询就非常重要了,开源界第一日志分析系统ELK必不可少. ELK系统由Elasticsearch(分布式 ...
- github提交代码后没有contribution问题
好气啊,好几天的代码提交后发现没有contribution,强迫症表示不能忍,仔细排查了下原因 可能有以下几个原因 1.提交代码的用户名和邮箱和账号不符合 方法1:git命令行设置用户名和邮箱 git ...