LeetCode: Generate Parentheses

本文介绍了通过递归和广度优先搜索算法解决括号生成问题的方法,提供了两种实现方式:C++版的递归算法和C#版的广度优先搜索算法。重点阐述了如何利用递归进行深度优先搜索,以及如何使用队列实现广度优先搜索来生成符合要求的括号组合。

第一次memory exceed了,想复杂了,看了答案才知道只要函数系数多点就行了。。

 1 class Solution {
 2 public:
 3     void dfs(int dep, int leftnum, int n, vector<string> &ret, int leftnumtotal, string tmp) {
 4         if (leftnumtotal > n) return;
 5         if (dep == 2*n) {
 6             ret.push_back(tmp);
 7             return;
 8         }
 9         dfs(dep+1, leftnum+1, n, ret, leftnumtotal+1, tmp+'(');
10         if (leftnum > 0)
11             dfs(dep+1, leftnum-1, n, ret, leftnumtotal, tmp+')');
12     }
13     vector<string> generateParenthesis(int n) {
14         // Start typing your C/C++ solution below
15         // DO NOT write int main() function
16         vector<string> ret;
17         string tmp = "";
18         if (!n) return ret;
19         dfs(0, 0, n, ret, 0, tmp);
20         return ret;
21     }
22 };

 也可以用下面这段bfs的算法

 1 struct par {
 2     string s;
 3     int total;
 4     int left;
 5     par() : s(""), total(0), left(0) { }
 6     par(string a, int b, int c) : s(a), total(b), left(c) { }
 7 };
 8     
 9 class Solution {
10 public:
11     vector<string> generateParenthesis(int n) {
12         // IMPORTANT: Please reset any member data you declared, as
13         // the same Solution instance will be reused for each test case.
14         if (n == 0) return vector<string>(0);
15         queue<par> que;
16         vector<string> res;
17         que.push(par("(", 1, 1));
18         while (!que.empty()) {
19             par front = que.front();
20             que.pop();
21             if (front.total == n && front.left == 0) res.push_back(front.s);
22             else {
23                 if (front.total < n) {
24                     que.push(par(front.s+"(", front.total+1, front.left+1));
25                     if (front.left > 0) que.push(par(front.s+")", front.total, front.left-1));
26                 }
27                 else que.push(par(front.s+")", front.total, front.left-1));
28             }
29         }
30         return res;
31     }
32 };

 C#

 1 public class par {
 2     public string s;
 3     public int total;
 4     public int left;
 5     public par(string a, int b, int c) { s = a; total = b; left = c; }
 6 }
 7 public class Solution {
 8     public List<string> GenerateParenthesis(int n) {
 9         List<string> ans = new List<string>();
10         if (n == 0) return ans;
11         Queue<par> que = new Queue<par>();
12         que.Enqueue(new par("(", 1, 1));
13         while (que.Count != 0) {
14             par peek = que.Peek();
15             que.Dequeue();
16             if (peek.total == n && peek.left == 0) ans.Add(peek.s);
17             else {
18                 if (peek.total < n) {
19                     que.Enqueue(new par(peek.s+"(", peek.total+1, peek.left+1));
20                     if (peek.left > 0) que.Enqueue(new par(peek.s+")", peek.total, peek.left-1));
21                 }
22                 else que.Enqueue(new par(peek.s+")", peek.total, peek.left-1));
23             }
24         }
25         return ans;
26     }
27 }
View Code

 

转载于:https://www.cnblogs.com/yingzhongwen/archive/2013/03/21/2973369.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值