LeetCode: Generate Parentheses

第一次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

 

你可能感兴趣的:(LeetCode)