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、如果左括号 '(' 数量少于n,则string末尾插入 '(';

  2、如果右括号数<左括号数,则string末尾插入 ')';

解题步骤:

1、主程序新建一个用于保存各种解的数组,并调用回溯函数;

2、回溯函数,输入为待填充的解空间lst、某一个解result、当前已填充的左括号数left,当前已填充的右括号数right,题目要求的括号数n

  (1)当result长度为n*2时,即已经完成n个括号的填充,则将result放入lst中,并结束递归;

  (2)否则,如果left<n,插入一个‘(’,继续下一层递归;

  (3)如果right<left,插入一个‘)’,继续下一层递归;

代码:

 class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> lst;
Backtracking(lst, "", , , n);
return lst;
} void Backtracking(vector<string> &lst, string result, int left, int right, int n) {
if (result.size() == n * ) {
lst.push_back(result);
return;
} if (left < n)
Backtracking(lst, result + '(', left+, right, n); if (right < left)
Backtracking(lst, result + ')', left, right+, n);
}
};

另:

//如何使用决策树实现,使叶子成为各种分配方式的集合?

【Leetcode】【Medium】Generate Parentheses的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  7. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. 【leetcode刷题笔记】Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. 【LeetCode每天一题】Valid Parentheses(有效的括弧)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  10. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

随机推荐

  1. dgango 报错: Timeout when reading response headers from daemon process

    问题: image = np.asarray(bytearray(f.read()), dtype="uint8")cv2_img = cv2.imdecode(image, cv ...

  2. selenium中等待元素的加载

    在实际使用中,有时会出现一个元素还没有加载出来,导致无法获取,此时需要selenium处理来等待一段时间,此时处理方法有以下几种: 1.最笨.最简单但是最不推荐的方式: Thread.sleep(ti ...

  3. CSS禁止滚动条

    CSS禁止滚动条的方法: 1.完全隐藏 在<boby>里加入scroll="no",可隐藏滚动条: <boby scroll="no"> ...

  4. 使用js命名空间进行模块式开发

    在java中,为了防止命名冲突和模块式开发,会有个一个import 关键字来进行导包. 在js中为了达到同样的效果,我们也通过给其自定义一个“包”的概念. 直接上代码: 首先有个LC.js文件: // ...

  5. java文件在没有安装jdk的windows下运行。

    1.首先该工程最好是gui的,使用swing或者awt的都行. 2.使用eclipse打包jar文件. 项目名字上面点右键,选择Export,在选择java\JAR file.    选择src文件夹 ...

  6. 【curl】Linux下命令行curl详解

    在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具. 语法:# c ...

  7. Tomcat热启动

    ===== 2017.7.1 ===== 如果是对原来的类方法修改,那么热启动非常好用:如果是添加了新的类或方法(非最上层的controller),那么此方法也是好用的:但是如果是在controlle ...

  8. WebDriverWait介绍

    转自:https://www.cnblogs.com/ella-yao/p/7778678.html WebDriverWait介绍 package com.test.elementwait; imp ...

  9. 远程桌面下启动MATLAB时的License Manager Error -103错误

    之前使用学校提供的正版lincense,是通过MATLAB 官网注册下载的MATLAB 2017a,远程登录可用打开.后来学校停止购买了,时间紧迫,网上下载了一个standalone的lincense ...

  10. Jquery+Aajax 批量上传

    注:转载请标明文章原始出处及作者信息 网上有现成的Uploadify.WebUpload等插件,自己写一个简单的(非插件). 1.页面 批量上传页面 <form action="&qu ...