LeetCode(22)GenerateParentheses
来源:互联网 发布:欧洲卡车模拟 mac 编辑:程序博客网 时间:2024/05/21 01:47
题目如下:
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:
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
题目意思是说,给出所有的合法的括号形式。比如,手动地来写写。
n=1时, "()"
n=2时,"()()", "(())"
n=3时,"((()))", "(()())", "(())()", "()(())", "()()()"
这个问题比较适合用递归来解决。经过一些探索,想到一个比较自然也比较好理解的办法。描述如下:
当n=1时,显然只能写出"()"
当n=2时,为什么写出了"()()", "(())",这和n=1时候的结果有什么关系呢?你可以看成有个基本元素element="()",它尝试把它本身放入上一次的结果中,也就是n=1的结果中。
当n=1时, 观察下面这个表达式
| ( | ) |
可以认为,对上面表达式中的三个竖线而言,每个竖线都是一个候选的插入位置。
如果把element="()" 插入竖线一,将构成()()
如果把element="()" 插入竖线二,将构成(())
如果把element="()" 插入竖线三,将构成()()
其中过滤掉重复,最后得到当n=2时,合法的括号形式为"()()", "(())"
把上面的思考写成code,如下
//// Solution.h// LeetCodeOJ_021_GeneratePairsOfParenthese//// Created by feliciafay on 12/18/13.// Copyright (c) 2013 feliciafay. All rights reserved.//#ifndef LeetCodeOJ_021_GeneratePairsOfParenthese_Solution_h#define LeetCodeOJ_021_GeneratePairsOfParenthese_Solution_h#include <vector>#include <iostream>#include <set>#include <string>#include <cmath>using namespace std;class Solution {public: vector<string> generateParenthesis(int n) { if(n<1){ vector<string> res_vec; return res_vec; } else if(n==1){ vector<string> res_vec; res_vec.push_back("()"); return res_vec; }else{ vector<string> pre_vec=generateParenthesis(n-1); set<string> filter_set; vector<string> res_vec; for(int i=0;i<pre_vec.size();i++){ string pre_str=pre_vec[i]; int pre_str_len=(int)pre_str.length(); for(int j=0;j<=pre_str_len;j++){ string now_str=pre_str.substr(0,j)+"()"+pre_str.substr(j,pre_str_len-j); if(filter_set.find(now_str)==filter_set.end()){ filter_set.insert(now_str); res_vec.push_back(now_str); } } string now_str=pre_str+"()"; if(filter_set.find(now_str)==filter_set.end()){ filter_set.insert(now_str); res_vec.push_back(now_str); } } return res_vec; } }};#endif
小结
(1) 一开始思考进入误区,写了很久输出结果总是不全。后来换了角度看问题,终于正确了。
(2) 这个题目背后的奥秘是卡特兰数,这个目前还没有开始看,稍后更新。
2 0
- LeetCode(22)GenerateParentheses
- week7-leetcode #22-GenerateParentheses
- LeetCode #22
- LeetCode 22: Generate Parentheses
- [leetcode 22] Valid Parentheses
- LeetCode 22 Surrounded Regions
- [leetcode 22] Generate Parentheses
- LeetCode | #22 Generate Parentheses
- [Leetcode] 22- Generate Parentheses
- LeetCode 22 Generate Parentheses
- [LeetCode 22]Generate Parentheses
- leetcode 22 Generate Parentheses
- LeetCode (22) Word Search
- Leetcode 22 Generate Parentheses
- LeetCode 22 - Generate Parentheses
- Generate Parentheses - LeetCode 22
- leetcode 22 -- Generate Parentheses
- leetcode-22Generate Parentheses
- fontconfig configure compile and install
- GridView翻页
- 黑马程序员_学习笔记第16天——Map集合
- Perl语言入门(第五版) 读书笔记(八)---字符串与排序
- Java中产生随机数的两个方法
- LeetCode(22)GenerateParentheses
- Terminator--在Ubuntu系统中同时打开多个终端窗口
- 数组、指针、指针数组、数组指针(转)
- c调用lua相关函数
- VSFTP 教程
- Ubuntu Locale配置问题根源解决之道
- 隐藏和恢复指定windows运行窗口
- 国际C语言混乱代码大赛----1988获奖作品赏析
- 通过BIOS设置启用CPU虚拟化解决VMware虚拟机不支持X86_64位linux操作系统安装问题