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 ...
随机推荐
- WP Super Cache 安装与设置方法
1.首先,永久连接不能使用默认格式 2.修改永久链接格式,中文推荐采用 /%post_id%.html (这下你知道我的.orz哪里来了吧) 如果你和我一样蛋疼愿意为每篇文章写一个英语的post sl ...
- git:hook declined FATAL: W refs/heads DENIED by fallthru error
hook declined FATAL: W refs/heads DENIED by fallthru error git提交代码时报错,网上查了,最终结果竟然是测试人员没有给我配置写的权限,配置了 ...
- python学习第一天 -安装配置及其输入输出
Python, 是一种面向对象.解释型计算机程序设计语言. python适合领域: 1.Web网络和各种网络服务 2.系统工具和脚本 3.作为“胶水”语言把其他语言开发的模块包装起来方便使用 pyth ...
- 答:我们公司的ASP.NET 笔试题,你觉得难度如何
闲来无事,逛逛园子,发现有个面试题,觉得有意思.已自己的理解答来看看,不足之处,请多指教. 原文地址:http://www.cnblogs.com/leotsai/p/aspnet-tests-for ...
- 迁移笔记:php截取文字的方法
php内置函数 1. iconv iconv_set_encoding('internal_encoding', 'UTF-8'); $str; //字符串的声明 $num=iconv_strlen( ...
- 动态修改ActionBar Menu的显示
应用场景: 在主Activity中,采用InstrumentedActivity侧边栏的方式,侧边栏的每一项对应一个Fragment,要实现不同的Fragment动态显示与隐藏ActionBar Me ...
- ubuntu 执行apt-get update 提示无法获得锁
问题如下: y@y:~$ sudo apt-get updateE: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用)E: 无法对目录 /var/l ...
- Smarty 变量使用
Smarty的标签都是使用定界符括起来. 默认定界符是{ 和 }, 但定界符可以被改变. 比如说在本手册,我们会假定你在使用默认的定界符. 在Smarty看来,任何在定界符之外的内容,都是静态的,或者 ...
- Php开发完全跨站点跨域名单点(SSO)同步登录和注销
From:http://www.cnblogs.com/JinkoWu/p/5056646.html 先来说说什么是单点登录(SSO).来自百科的介绍:SSO英文全称Single Sign On,单点 ...
- paip.输入法编程---智能动态上屏码儿长调整--.txt
paip.输入法编程---智能动态上屏码儿长调整--.txt 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csd ...