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 ...
随机推荐
- 在Express中使用Multiparty进行文件上传及POST、GET参数获取
Express 版本:4.14.1 在Express中,文件上传需要用到multiparty中间件,在项目目录中,通过npm install multiparty –save进行安装必要组件. 前端H ...
- 廖雪峰Java5Java集合-5Queue-1使用Queue
Queue特性和基本方法 Queue实现一个先进先出(FIFO, First In First Out)的队列.如收银台排队支付. Java中LinkedList实现了Queue接口,可以直接把Lin ...
- 在外部怎么调用jquery插件里的function
文章来源:百度知道 问:(function($){函数(){xxxx}})(jQuery),我怎么调用这个函数呢? (function($){ function render(jq){ 这里是jque ...
- ntp服务问题
原本国内的主机直接指向阿里云就可以时间同步了 但是国外的主机 却有报错 这个报错还没有解决 1 Oct 03:47:30 ntpdate[20969]: no server suitable fo ...
- sqlite之多线程总结
12.android 多线程数据库读写分析与优化 11.多线程操作Sqlite? ==== 11.android 多线程数据库读写分析与优化 最新需要给软件做数据库读写方面的优化,之前无论读写,都是用 ...
- java使用freemarker模板导出word(带有合并单元格)文档
来自:https://blog.csdn.net/qq_33195578/article/details/73790283 前言:最近要做一个导出word功能,其实网上有很多的例子,但是我需要的是合并 ...
- java读取按行txt文件
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; pub ...
- Delphi2010如何获取程序内部自身版本号?
用原来的GetFileVersionInfo只能获取Delpi7的程序版本号,用在Delphi2010中就不管用了 //------ 获取文件版本号function F_GetFileVersion( ...
- python-day02-购物车
购物车 需求: 1.启动程序后,让用户输入工资,然后打印商品列表: 2.容许用户根据商品编号购买商品: 3.用户选择商品后,检测余额是否足够,够了就直接扣款,不够就提醒客户: 4.随时可以退出,退出时 ...
- 获取图片src
用jquery获取图片src(不知道为什么总是undefined): $('img').eq(i).src; $('img').eq(i).attr('src'); //上面两种都是获取不到,不知道为 ...