LeetCode:Generate Parentheses
题目链接
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
所有组合的个数其实是一个卡特兰数。
我们这样来构造一个合法的字符串:首先第一个位置肯定是“(”,对于后面位置:
1、如果前一个字符是“(”:那么我们可以在当前位置上加入“)”(字符“)”一定有剩余);如果“(”字符还有剩余,也可以在当前位置加入“(” 本文地址
2、如果前一个字符是“)”:如果当前“(”和“)”剩余的数目不同(即其那面的括号没有完全匹配),可以在当前位置上加入“)”;如果“(”字符还有剩余,也可以在当前位置加入“(”
1 class Solution { 2 public: 3 vector<string> generateParenthesis(int n) { 4 string tmpres(n<<1, '('); 5 vector<string> res; 6 helper(1, tmpres, res, n-1, n); 7 return res; 8 } 9 private: 10 void helper(int index, string &tmpres, vector<string>&res, int leftNum, int rightNum) 11 { 12 if(index >= tmpres.size()){res.push_back(tmpres); return;} 13 if(tmpres[index-1] == '(') 14 { 15 tmpres[index] = ')'; 16 helper(index+1, tmpres, res, leftNum, rightNum-1); 17 if(leftNum > 0) 18 { 19 tmpres[index] = '('; 20 helper(index+1, tmpres, res, leftNum-1, rightNum); 21 } 22 } 23 else 24 { 25 if(leftNum != rightNum) 26 { 27 tmpres[index] = ')'; 28 helper(index+1, tmpres, res, leftNum, rightNum-1); 29 } 30 if(leftNum > 0) 31 { 32 tmpres[index] = '('; 33 helper(index+1, tmpres, res, leftNum-1, rightNum); 34 } 35 } 36 } 37 };
其实对于某个合法的字符串,我们可以发现从合法字符串的任何一个位置看,“(”的数目 >= ")"的数目,即剩余的“(”的数目 <= 剩余的")"数目, 因此就有以下更加简洁的代码
1 class Solution { 2 public: 3 vector<string> generateParenthesis(int n) { 4 string tmpres; 5 vector<string> res; 6 helper(tmpres, res, n, n); 7 return res; 8 } 9 private: 10 void helper(string &tmpres, vector<string>&res, int leftNum, int rightNum) 11 { 12 if(leftNum > rightNum)return; 13 if(leftNum == 0 && rightNum == 0) 14 { 15 res.push_back(tmpres); 16 return; 17 } 18 if(leftNum > 0) 19 { 20 tmpres.push_back('('); 21 helper(tmpres, res, leftNum-1, rightNum); 22 tmpres.pop_back(); 23 } 24 if(rightNum > 0) 25 { 26 tmpres.push_back(')'); 27 helper(tmpres, res, leftNum, rightNum-1); 28 tmpres.pop_back(); 29 } 30 } 31 };
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3776583.html
转载于:https://www.cnblogs.com/TenosDoIt/p/3776583.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 parenthes ...
- [leetcode]Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- [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 ...
最新文章
- FPGA之道(26)VHDL初始化
- bat 批量提取指定目录下的文件
- angularjs 1.x $q模块使用
- VTK修炼之道83:Pipeline管线执行模型
- 漫话:如何给女朋友解释什么是HTTP
- java 8 stream_Java 8 Stream示例
- C++——C++11中的defalut和delete关键字
- 小米8青春版超级夜景安排上了 这个样张我是服气的!
- 宝马无法gps定位_2.0T+后驱,豪华品牌论运动还得看它,带你看宝马3系
- <笠翁对韵>全文及译文(上卷)
- 【Unity】在Scene窗口中发射射线
- PhpStorm 2019 for mac(PHP集成开发工具) 2019.1.3中文激活版
- PowerPoint.Application win32 操作ppt 复制 新建 插入图片
- 堡垒机AccessClient插件在mac系统下闪退的解决办法
- 未来微型计算机发展趋势,简述什么是单片机(单片机未来发展趋势)
- NFC NFC手机 兼容的标签 支持Mifare Classic 手机列表
- PLY文档翻译——利用Python进行词法和语法分析
- 挑战感知极限:智能安全感知驱动设计
- django 进阶第二天 生鲜超市学习 model
- 中国GMP级细胞因子市场现状及未来发展趋势