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 ...
随机推荐
- mysql查询优化之四:优化特定类型的查询
本文将介绍如何优化特定类型的查询. 1.优化count()查询count()聚合函数,以及如何优化使用了该函数的查询,很可能是mysql中最容易被误解的前10个话题之一 count() 是一个特殊的函 ...
- 廖雪峰Java5集合-4Set-1使用Set
集合 Set用于存储不重复的元素集合: boolean add(E e) boolean remove(Object o) boolean contains(Object o) int size() ...
- Oracle group by
group by 简单点理解就是根据什么分组 为此 group by job 根据job进行分组 举例:在Oracle当中scott/oracle 下有emp表 进行如下操作 体会order b ...
- 阿里云线上ROS静态路由转发,有大坑。
原因见上去,阿里云不支持VPC中转流量,VPC1和VPC2都在国内,VPC3在香港,如果按阿里云的做法,必须付费2次国际隧道的钱,才可以实现三个VPC互通.明显很浪费钱. 所以我们只能在三个VPC,各 ...
- 在 Element-UI 的 Table 组件上添加列拖拽效果
Element-UI 的 Table组件很强大,但是我们的需求更强大... 简单粗暴的来一发效果图: 一.数据驱动 传统的拖动效果,都是基于通过 mousedown.mousemove.mouseup ...
- windows server 2008 R2 安装
微软服务器操作系统大致有: server 2000(简称2K),还有server 2003(2K3),server 2008(2K8),server 2000和2003是基于NT内核的,而2008是基 ...
- [UE4]关于分支Sequence和条件分支的组合用法
当需要不管条件语句是否成立的后面都需要执行的语句,可以使用“Sequence”来分支,达到简化蓝图连线的目的.如下图所示:
- 字符串格式化format使用
顺序传参 '{}....{}'.format(value1, value2) 索引传参 '{0}....{1}'.format(value1, value2) 关键字传参 '{k1}....{k2}' ...
- 修改IP和DNS的dos命令
修改IP,掩码,网关命令: netsh interface ip set address "本地连接" static 192.168.3.188 255.255.255.0 192 ...
- spark使用hadoop native库
默认情况下,hadoop官方发布的二进制包是不包含native库的,native库是用C++实现的,用于进行一些CPU密集型计算,如压缩.比如apache kylin在进行预计算时为了减少预计算的数据 ...