生成括号-LintCode
来源:互联网 发布:淘宝大悲咒水晶杯真假 编辑:程序博客网 时间:2024/05/29 08:46
给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。
样例:
给定 n = 3, 可生成的组合如下:
“((()))”, “(()())”, “(())()”, “()(())”, “()()()”
思路:
递归。
#ifndef C427_H#define C427_H#include<iostream>#include<vector>#include<string>#include<algorithm>using namespace std;class Solution {public: /* * @param n: n pairs * @return: All combinations of well-formed parentheses */ vector<string> generateParenthesis(int n) { // write your code here vector<string> res; if (n <= 0) return res; if (n == 1) return{ "()" }; vector<string> vstr = generateParenthesis(n - 1); for (auto c : vstr) { for (int i = 0; i < 2*(n-1); ++i) { for (int j = i+1; j <= 2*(n-1); ++j) { string str = c; str.insert(i, "("); str.insert(j, ")"); res.push_back(str); } } } sort(res.begin(), res.end()); auto iter = unique(res.begin(), res.end()); res.erase(iter, res.end()); return res; }};#endif
阅读全文
0 0
- LintCode : 生成括号
- LintCode-生成括号
- LintCode:生成括号
- LintCode:生成括号
- lintcode,生成括号
- 生成括号-lintcode
- lintcode -- 生成括号
- LintCode 生成括号
- DFS-lintcode生成括号
- 生成括号-LintCode
- lintcode刷题--生成括号
- lintcode--生成括号
- [Lintcode]Generate Parentheses 生成括号
- Generate Parentheses 生成括号-- LintCode题解
- lintcode括号匹配
- 生成括号
- 生成括号
- 生成括号
- MySQL--内连接、左外连接、右外连接
- JMETER:linux进行分布式压力测试
- 【第七周项目5】排队看病模拟
- 判断是移动端还是pc端
- python模块导入问题和if __name__ == '__main__'语句的使用
- 生成括号-LintCode
- 7-43 Shuffling Machine(20 分)
- PAT 1009. 说反话 (20)
- linux系统编程之进程(八):守护进程详解及创建,daemon()使用
- linux下的phpredis安装
- Python的错误
- 还记的那年的加州招待所吗?
- cmd常用命令
- hadoop存储hdfs磁盘余留问题