【Leetcode】【Medium】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、如果左括号 '(' 数量少于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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【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 ...
- 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode刷题笔记】Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode每天一题】Valid Parentheses(有效的括弧)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【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 ...
随机推荐
- 【爬虫】-爬取猫眼TOP100
原文崔庆才<python3网络爬虫实战> 本文为自学记录,如有侵权,请联系删除 目标: 熟悉正则表达式,以及爬虫流程 获取猫眼TOP100榜单 1.网站分析 目标站点为http://www ...
- Ubuntu下配置安装Hadoop 2.2
---恢复内容开始--- 这两天玩Hadoop,之前在我的Mac上配置了好长时间都没成功的Hadoop环境,今天想在win7 虚拟机下的Ubuntu12.04 64位机下配置, 然后再建一个组群看一看 ...
- (转)nginx日志配置指令详解
这篇文章主要介绍了nginx日志配置指令详解,nginx有一个非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志,需要的朋友可以参考下日志对于统计排错来说非常有利的.本文总结了nginx日 ...
- spring data jpa关联查询(一对一、一对多、多对多)
在实际过往的项目中,常用的查询操作有:1.单表查询,2.一对一查询(主表和详情表)3.一对多查询(一张主表,多张子表)4.多对多查询(如权限控制,用户.角色多对多).做个总结,所以废话不多说. 使用i ...
- 参数化登录QQ空间实例
通过参数化的方式,登录QQ空间 实例源码: # coding:utf-8 from selenium import webdriver import unittest import time clas ...
- pow() 函数
pow() 函数用来求 x 的 y 次幂(次方),其原型为: double pow(double x, double y); #include<iostream> #include< ...
- 九度oj 1468 Sharing 2012年浙江大学计算机及软件工程研究生机试真题
题目1468:Sharing 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2687 解决:550 题目描述: To store English words, one method is ...
- Nginx几个简单命令
• 启动nginx服务 sudo brew services start nginx 利用http://localhost:8080进行访问, 如果出现如下界面,说明启动成功. • 查看nginx版本 ...
- hosts文件配置参数介绍
hosts文件配置参数介绍 1, ansible_ssh_host : 指定主机别名对应的真实 IP,如:100 ansible_ssh_host=192.168.1.100,随后连接该主机无须指定完 ...
- js验证身份证号码是否合规
需求:最近要做实名验证的功能,但是验证身份证号码和身份证图片的接口不想短信,比较贵,所以之前我们要验证严谨一点,参考了网上关于验证身份证号码的代码,总结一下 代码: //验证身份证号码 functio ...