[LeetCode] Generate Parentheses
For example, given n = 3, a solution set is:
["((()))","(()())","(())()","()(())","()()()" ]
括号匹配。使用递归的方法。
设定left、right表示左右括号的剩余数,
如果left > right表示匹配失衡,直接返回。
如果left < right表示已经存在一个左括号,需要匹配下一次左括号或者右括号,进行递归。
如果left == right == 0,表示括号匹配完毕且合法。把当前括号序列放入结果数组中。
参考代码如下:
class Solution { public:vector<string> generateParenthesis(int n) {vector<string> res;generateParenthesis(n, n, "", res);return res;}void generateParenthesis(int left, int right, string str, vector<string>& res){if (left > right){return;}if (left == 0 && right == 0){res.push_back(str); }else{if (left > 0){generateParenthesis(left-1, right, str+'(', res);}if (right > 0){generateParenthesis(left, right-1, str+')', res);}}} };
另一种方法,提前剪枝,思路更清晰,left、right表示当前左右括号的数量
class Solution { public:vector<string> generateParenthesis(int n) {vector<string> res;string out;dfs(n, 0, 0, out, res);return res;}void dfs(int n, int left, int right, string out, vector<string>& res){if (left < n){out.push_back('(');dfs(n, left+1, right, out, res);out.pop_back();}if (left > right){out.push_back(')');dfs(n, left, right+1, out, res);out.pop_back();}if (out.size() == n*2)res.push_back(out);} };
转载于:https://www.cnblogs.com/immjc/p/9454370.html
[LeetCode] Generate Parentheses相关推荐
- LeetCode Generate Parentheses
原题链接在这里:https://leetcode.com/problems/generate-parentheses/ 题目: Given n pairs of parentheses, write ...
- [leetcode]Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- LeetCode:Generate Parentheses
题目链接 Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- [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 ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【LeetCode从零单排】No22.Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [LeetCode] #22 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
最新文章
- 2019年顶级软件开发趋势
- nginx 修复固定链接404
- 简单搜索(多位自幂数)+数列网站
- 为何要离开?该怎么离开?今天来谈谈辞职
- Bootstrap3系列:下拉菜单
- Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)
- threejs骨架形状
- 特征计算 - Jaccard 相似系数与 Python 代码实现
- 使用 jQuery Mobile 与 HTML5 开发 Web App (十) —— jQuery Mobile 默认配置与事件基础
- 小型自动化运维--expect脚本之传递函数
- 求n!最后一位非零数
- C#对IE使用Proxy(代理)
- php百度蜘蛛劫持,技术教程:php伪造ip访问一个网站,可以伪造百度蜘蛛ip
- 游戏美术师的火绝对不是捧出来的!不看不知道游戏模型师这么吃香
- KVM虚拟化的概述和部署
- Vue3 suspense
- 分享一个免费的OCR图片文字识别接口
- 从零写一个操作系统之booting
- 解决高分辨率下安装Linux花屏问题
- 记一次实验报告:基于Linux的中小型企业网络架构
热门文章
- 个人笔记:ORACLE大页内存hugepage和SGA、PGA的经验,SGA并不是越大越好
- 46. Permutations 排列数
- 01【在线日志分析】之Flume-1.7.0源码编译导入eclipse
- 计算广告学学习2 - 广告有效性模型
- 比select2 更好用的chosen插件 for angular
- C# 复制 粘贴 剪切 撤销
- JScript 和 VBscript访问网络上的打印设备
- .NET项目是否有必要升级到.NET 3.5 + VS 2008
- 你必须失败---来自迈克尔·乔丹的6条教训
- .Net内存管理、垃圾回收