【一天一道LeetCode】#22. Generate Parentheses
一天一道LeetCode
(一)题目
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:
“((()))”, “(()())”, “(())()”, “()(())”, “()()()”
(二)解题
在【一天一道LeetCode】#20. Valid Parentheses一文中提到怎么判断该组括号有效,另外在【一天一道LeetCode】#17. Letter Combinations of a Phone Number一文中用到回溯法。本题结合这两题的思路来解决。
假设在位置k之后,如果’(‘的数量还有剩余则可以继续添加;那么要添加’)’,则必须满足其剩余的数量大于0且比’(‘剩余的数量大,才可以添加,否则就不是一个有效的括号组。
class Solution {
public:
vector<string> ret;
vector<string> generateParenthesis(int n) {
string str;
generate(str , n , n);
return ret;
}
void generate(string str , int m , int n)
{
if(m==0)//
{
while(n--) str+=')';//如果‘(’没有剩余的了,就直接把剩余的')'加上
ret.push_back(str);
return;
}
if(m>0)//如果‘(’剩余,可以继续添加'('
{
generate(str+'(',m-1,n);
}
if(m<n&&n>0)//如果m<n且n>0,可以继续添加')'
{
generate(str+')',m,n-1);
}
}
};
【一天一道LeetCode】#22. Generate Parentheses的更多相关文章
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- LeetCode 22. Generate Parentheses
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生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [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 ...
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
- [leetcode] 22. Generate Parentheses(medium)
原题 思路: 利用DFS,搜索每一种情况,同时先加"("后加")",保证()匹配正确. 最近开始学习前端,尝试用js来写. const generate = f ...
随机推荐
- Shell脚本了解
一.什么是Shell Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面, ...
- 安卓框架——SlidingMenu使用技巧
SlidingMenu的一些常用属性 原文转载http://blog.csdn.net/zwl5670/article/details/48274109 [java] view plain copy ...
- Objective-C's Init Method
初始化器在其他面向对象的语言中(比如Java)指的是构造器. Objective-C同样拥有对象构造器在init形式的方法中.不管如何,在Objc中这些方法没有什么特殊的行为. 按照惯例,程序猿在in ...
- scheme深拷贝和浅拷贝探索
> (define a '(1 2 3)) > (define b (cons a '())) > b (( )) > (set-car! (car b) ) > b ( ...
- 2014 BDTC 参会有感
中国大数据技术大会(Big Data Technology Conference,BDTC)是目前国内最具影响.规模最大的大数据领域的技术盛会.大会的前身是Hadoop中国云计算大会(Hadoop i ...
- linux简单命常用令
Linux常用命令总结 切换:cd tmp cd/tmp/yun cd 切换到host目录 cd .. 显示:ll Top显示系统情况 Netstat显示网络情况 Ifconfig显示网络配置 Mor ...
- [Mysql]mysql windows下配置文件
环境是win7 mysql5.6版本 测试下配置文件是否可用(之前没用过windows下的msyql配置) 修改配置前查询下: mysql> show variables like '%max_ ...
- 集合框架之Collections静态工具类
Collections类提供了一些列静态的方法,用以更方便地操作集合类 排序机制 一个List可以通过下面的方法进行排序: Collections.sort(list); 如果List包含的是字符串, ...
- 安卓自定义View实现图片上传进度显示(仿QQ)
首先看下我们想要实现的效果如下图(qq聊天中发送图片时的效果): 再看下图我们实现的效果: 实现原理很简单,首先我们上传图片时需要一个进度值progress,这个不管是自己写的上传的方法还是使用第三方 ...
- 14 Fragment 注意点
API 过时问题 API 23过时 public void onAttach(Activity activity)替换为public void onAttach(Context context) 注意 ...