Leetcode: Generate parentheses

简介:

输入n,输出n个左括号和n个右括号的合法括号序列

关键:当前位置的左括号不少于右括号

图:

  节点:当前位置左括号和右括号的个数(x, y)(x>=y)

  边:从(x, y)到(x+1, y)或(x,y+1)

  x==y时,只有(x+1,y)这条边

解:从(0,0)出发到(n,n)的全部路径

====================

C++

DFS

记录什么

  左右括号的个数

  当前的部分解

复制代码
 1 class Solution {
 2 public:
 3     void helperDFS(int n, int x, int y, string now, vector<string> &ans) {
 4         if (y == n) {
 5             ans.push_back(now);
 6             return;
 7         }
 8         if (x < n) {
 9             helperDFS(n, x + 1, y, now + "(", ans);
10         }
11         if (x > y) {
12             helperDFS(n, x, y + 1, now + ")", ans);
13         }
14         
15     }
16     vector<string> generateParenthesis(int n) {
17         vector<string> ans;
18         helperDFS(n, 0, 0, "", ans);
19         return ans;
20     }
21 };
复制代码

C++

BFS

  记录什么

  方法1:当前的部分解

  方法2:每个节点记录能到达它之前的节点集合(麻烦,最后还要还原路径)

复制代码
 1 struct node{
 2     int x, y;
 3     string now;
 4 };
 5 class Solution {
 6 public:
 7     void helperBFS(int n, vector<string> &ans) {
 8         if (0 == n) {
 9             ans.push_back("");
10             return;
11         }
12         node tmp;
13         tmp.x = tmp.y = 0;
14         tmp.now = "";
15         queue<node> q;
16         for (q.push(tmp); !q.empty(); q.pop()) {
17             tmp = q.front();
18             node other;
19             if (tmp.x < n) {
20                 other.x = tmp.x + 1;
21                 other.y = tmp.y;
22                 other.now = tmp.now + "(";
23                 q.push(other);
24             }
25             if (tmp.x > tmp.y) {
26                 other.x = tmp.x;
27                 other.y = tmp.y + 1;
28                 other.now = tmp.now + ")";
29                 if (other.y == n) {
30                     ans.push_back(other.now);
31                 } else {
32                     q.push(other);
33                 }
34             }
35         }
36         
37     }
38     vector<string> generateParenthesis(int n) {
39         vector<string> ans;
40         helperBFS(n, ans);
41         return ans;
42     }
43 };
复制代码

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5012102.html,如需转载请自行联系原作者

相关文章
LeetCode 301. Remove Invalid Parentheses
删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。 说明: 输入可能包含了除 ( 和 ) 以外的字符。
48 0
LeetCode 301. Remove Invalid Parentheses
LeetCode 20:有效的括号 Valid Parentheses
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 有效字符串需满足: 左括号必须用相同类型的右括号闭合。
740 0
|
Java
[LeetCode] Valid Parentheses 验证括号是否有效闭合
链接:https://leetcode.com/problems/valid-parentheses/#/description难度:Easy题目:20.
981 0
LeetCode - 32. Longest Valid Parentheses
32. Longest Valid Parentheses  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个由'('和')'组成的字符串,求最长连续匹配子串长度.
946 0
LeetCode - 20. Valid Parentheses
20. Valid Parentheses  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个括号序列,检查括号是否按顺序匹配.
847 0
|
1月前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2

热门文章

最新文章