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 ...
随机推荐
- [UE4]游戏主循环
游戏的运行模型 理解游戏的运行模型,对处理很多游戏错误有非常大的帮助. 游戏是有一个主循环的.那么游戏主循环做了什么事情呢? 游戏主循环一次就表示一帧,游戏主循环包括:接受输入.处理游戏逻辑.渲染.S ...
- Blob下载文件 & 模拟滚动条实现
1.vue切换路由视图时,事件钩子顺序是 当前模块create-->上一个模块beforeDestroy-->当前模块mounted 因此注册全局事件(比如给window注册事件)应放在m ...
- 00002 - echo命令详解
用于字符串的输出 格式 echo string 使用echo实现更复杂的输出格式控制 1.显示普通字符串: echo "It is a test" 这里的双引号完全可以省略,以下命 ...
- 配置ssh服务允许root管理员直接登录
配置ssh服务允许root管理员直接登录 [root@linux-node2 ~]# grep PermitRootLogin /etc/ssh/sshd_config PermitRootLogin ...
- 第1章 计算机网络和协议(3)_TCP/IP协议
3. TCP/IP协议 3.1 TCP/IP协议分层 3.2 TCP/IP通信过程 (1)应用层:浏览器和Web服务器是两个对等的实现,它们之间使用http协议进行通信. (2)传输层:网页传输之前, ...
- js原生面向对象-仿layui选项卡
喜欢琢磨,给大家分享小编自己封装的仿layui的选项卡. <!DOCTYPE html> <html lang="en"> <head> < ...
- 反射中的BindingFlags
不指定绑定标志 BindingFlags.Default 表示忽略 name 的大小写,不应考虑成员名的大小写 BindingFlags.IgnoreCase 只应考虑在所提供类型的层次结构级别 ...
- mapreduce运行原理及YARN
mapreduce1回顾 mapreduce1的不足 yarn的基本架构 yarn工作流程
- WPF 自定义分页控件二
一:添加自定义分页控件,命名为KDataPagerTwo: public class KDataPagerTwo : Control, INotifyPropertyChanged { static ...
- python2.7 urllib2 爬虫
# _*_ coding:utf-8 _*_ import urllib2import cookielibimport randomimport refrom bs4 import Beautiful ...