22. Generate Parentheses
https://leetcode.com/problems/generate-parentheses/
题目大意:给出n对小括号,求出括号匹配的情况,用列表存储并返回,例如:n=3时,答案应为:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
解题思路:想到递归的思想,判断匹配成功(递归返回)的条件为:左右括号数保持一致并且括号字符串的长度等于2*n。
具体做法:需要的空间:字符串列表存储最后结果;string存储括号;n存储要求的括号对数;deep存储字符串长度;left存储已经存入的左括号个数;right存储已经存入的右括号个数。
解题技巧:递归实现时一般给出的函数难以进行递归,需要一个帮助函数,我们重新定义一个函数来实现递归。
注意:s.resize(s.size() - 1);该句话是为了实现回溯,考虑一下每次执行该句时上一句的递归一定已经实现了,所以需要去除最后一个括号
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
string s;
if(n > )
{
generateParenthesisHelper(result, s, n, , , );
}
return result;
}
void generateParenthesisHelper(vector<string>& result, string &s, int n, int deep, int left, int right)
{
if(deep == *n)
{
result.push_back(s);
return;
}
if(left < n)
{
s.push_back('(');
generateParenthesisHelper(result, s, n, deep + , left + , right);
s.resize(s.size() - );
}
if(right < left)
{
s.push_back(')');
generateParenthesisHelper(result, s, n, deep + , left, right +);
s.resize(s.size() - );
} } };
22. Generate Parentheses的更多相关文章
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 22. Generate Parentheses (recursion algorithm)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 22. Generate Parentheses——本质:树,DFS求解可能的path
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
随机推荐
- 使用 OpenWrt Image Generator 为 WR703N 路由器定制固件
标题:使用 OpenWrt Image Generator 为 WR703N 路由器定制固件 之前试着自己编译固件,编译是成功了,但是在后期安装官方仓库的ipk时出现问题,因为自己编译的固件和官方固件 ...
- Spring Data JPA之Hello World
Spring Data Jpa 配置 使用 Spring Data JPA 进行持久层开发需要的四个步骤: 1.配置 Spring 整合 JPA 2.在 Spring 配置文件中配置 Spring D ...
- 用jquery可以用使用serialize()序列化表单值,那有没有什么方法可以将值填充到表单中呢? (一段不错的代码)
serialize()的作用,是生成一个类似这种格式的字符串用于ajax提交 a=&b=&c=.你想将值填写到表单,首先要有值,然后就是表单控件的id或者能唯一定位控件的属性.然后就$ ...
- 搜索Collections元素,用DateFormatSymbols 获得月份
import java.util.Collections; import java.util.List; import java.text.DateFormatSymbols; import java ...
- Speech Module
Speech Module 1 FIRST_TEN = ["one", "two", "three", "four", ...
- Tk::Table
<pre name="code" class="python"># DESCRIPTION # Tk::Table is an all-perl w ...
- 发几个速度快可以用的google IP,谷歌IP(转)
google搜索引擎打不开时的解决办法,谷歌(google)的IP是多少? google IP镜像. 这里搜集了几个经过测试可用的IP,用来在不能域名访问google的时候进行访问,实时更新! 前面几 ...
- mysql 中 LIMIT的简单用法
mysql--语法: SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset --举例: select * from table l ...
- Paint House 解答
Question There are a row of n houses, each house can be painted with one of the three colors: red, b ...
- Search in Rotated Sorted Array (I, II) 解答
Question Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 ...