zoukankan      html  css  js  c++  java
  • [LeetCode] 22

    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:

    "((()))", "(()())", "(())()", "()(())", "()()()"

    class Solution {
    public:
      void doGenerate(int x0, int x1, string&& s) {
        if (!x0 && !x1) {
          m_result.emplace_back(s);
        }
        if (x0 > 0) {
          doGenerate(x0 -1, x1, s + "(");
        }
        if (x1 > 0 && x1 > x0) {
          doGenerate(x0, x1 - 1 , s + ")");
        }
      }

      vector<string> generateParenthesis(int n) {
        int x0 = n, x1 = n;
        m_result.clear();
        doGenerate(n, n, "");
        return m_result;
      }
    private:
      vector<string> m_result;
    };

  • 相关阅读:
    小程序注册
    Webpack
    npm总结1
    js事件
    js高级程序2
    js高级程序
    索引
    将数据渲染到页面的方法
    前后端分离后,通讯问题 springboot + vue
    axios post 请求后端参数为null解决方案
  • 原文地址:https://www.cnblogs.com/shoemaker/p/4769202.html
Copyright © 2011-2022 走看看