zoukankan      html  css  js  c++  java
  • [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 class Solution {
     2 public:
     3     void getRes(vector<string> &res, int st, string str, int count, int n) {
     4         if (str.length() > 2 * n || st > n) return;
     5         if (count == n && st == 0) {
     6             res.push_back(str);
     7             return;
     8         }
     9         getRes(res, st + 1, str + '(', count, n);
    10         if (st > 0) getRes(res, st - 1, str + ')', count + 1, n);
    11     }
    12     
    13     vector<string> generateParenthesis(int n) {
    14         vector<string> res;
    15         getRes(res, 0, "", 0, n);
    16         return res;
    17     }
    18 };
  • 相关阅读:
    poj1087最大流拆点
    3月15上午的函数练习
    3月15
    3月13上午
    3月13
    3月12
    break语句
    3月11
    3月10号
    3月9号
  • 原文地址:https://www.cnblogs.com/easonliu/p/3649601.html
Copyright © 2011-2022 走看看