[CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses.
EXAMPLE
Input: 3
Output: ((())), (()()), (())(), ()(()), ()()()
LeetCode上的原题,请参见我之前的博客Generate Parentheses 生成括号。
解法一:
class Solution { public:vector<string> generateParens(int n) {set<string> t;if (n == 0) t.insert("");else {vector<string> pre = generateParens(n - 1);for (auto a : pre) {for (int i = 0; i < a.size(); ++i) {if (a[i] == '(') {a.insert(a.begin() + i + 1, '(');a.insert(a.begin() + i + 2, ')');t.insert(a);a.erase(a.begin() + i + 1, a.begin() + i + 3);}}t.insert("()" + a);}}return vector<string>(t.begin(), t.end());} };
解法二:
class Solution { public:vector<string> generateParens(int n) {vector<string> res;generateParensDFS(n, n, "", res);return res;}void generateParensDFS(int left, int right, string out, vector<string> &res) {if (left > right) return;if (left == 0 && right == 0) res.push_back(out);else {if (left > 0) generateParensDFS(left - 1, right, out + '(', res);if (right > 0) generateParensDFS(left, right - 1, out + ')', res);}} };
转载于:https://www.cnblogs.com/grandyang/p/4828614.html
[CareerCup] 9.6 Generate Parentheses 生成括号相关推荐
- 递归/回溯:Generate Parentheses生成合法括号
已知n组括号,开发一个程序,生成这n组括号所有的合法的组合可能. 例如:n = 3 结果为: ["((()))", "(()())", "(())() ...
- LeetCode 22. 括号生成(Generate Parentheses)
题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: ["((()))","(()() ...
- [leetcode] 22. Generate Parentheses
题目大意 https://leetcode.com/problems/generate-parentheses/description/ 22. Generate Parentheses Given ...
- LeetCode算法入门- Generate Parentheses -day16
LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...
- python:实现balanced parentheses平衡括号表达式算法(附完整源码)
python:实现balanced parentheses平衡括号表达式算法 from .stack import Stack def balanced_parentheses(parentheses ...
- 记一次在nuxt中使用generate批量生成静态站点页面
做过seo的同学都知道,一些不是实时变化的页面(比如一个星期更新一次内容)我们可以生成静态的站点去让爬虫去爬去,这样就无需一次次地调用接口了. 那么在nuxt中我们怎么去做呢? nuxt为我们提供了一 ...
- android studio 使用Generate JavaDoc生成javaDoc
通过android studio tool 下的Generate JavaDoc 生成javaDoc,可以选择整个项目,也可以选择某个moudle 或者某个路径进行生成,如果你想要生成的javaDoc ...
- LeetCode22——Generate Parentheses(给定n对括号,然后看有多少正确的括号组合)
题目: 解法: The idea is intuitive. Use two integers to count the remaining left parenthesis (n) and the ...
- 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
最新文章
- 面试必备:6个你必须熟练掌握的机器学习算法
- sdcms的一个bug,总是提示,该文件不允许被上传
- 语言中2000u等于多少_PLC文本语言
- java-回调机制详解
- java map用二叉树_【课堂笔记分享】linkedlist、二叉树、hashmap
- 使用Swagger UI的Document和Test API
- RFID芯片有什么用
- android微信第三方登录怎么通过code获取openid?
- java 可以直接当自定义标示符_JAVA 从头开始二
- 一键安装服务器系统,一键安装服务器系统
- 现代电力系统分析[简答】
- mysql毫秒丢失_MySQL JDBC 更新数据丢失毫秒精度
- 云计算-平台架构-开源-OpenStack
- 如何预测用户query意图 « 搜索技术博客-淘宝
- 【北京-亚运村】这7家公司推荐给你
- 什么是 Web 3.0:面向未来的去中心化互联网
- 贪婪洞窟2一直显示连接服务器失败,《贪婪洞窟2》登陆失败解决方法 进不去怎么解决...
- 京东数据分析工具,同行商家数据快速查看对比
- 什么是哈希冲突?如何解决?
- 求生之路服务器参数配置
热门文章
- android 画圆教程,android shap画圆(空心圆、实心圆)
- html监控用户在线与离线,HTML5判断设备在线离线及监听网络状态变化例子
- 批量提取文件创建时间_批量采集新浪微博用户内容
- python 字符串分割_如何使用python语言split方法对不同字符串分割
- android 注解点击事件,android click事件注解
- idea 找不到或无法加载主类_解决IDEA中Groovy项目no Groovy library is defined的问题
- air什么意思中文_Air 的中文意思是什么?
- python处理csv文件列错位_CSV文件分割与列异常处理的python脚本
- 多模光纤收发器的基本参数及主要特点
- 音频光端机与电话光端机区别