【一天一道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 ...
随机推荐
- Cloudera: Start Impala service by cloudera manager in docker quickstart image
How to start Impala service in docker quickstart image docker run --hostname=quickstart.cloudera --p ...
- Gradle 1.12用户指南翻译——第46章. Java 库发布插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- 带有进度条的WebView
带有进度条的WebView 本篇继于WebView的使用 效果图 自定义一个带有进度条的WebView package com.kongqw.kbox.view; import android.con ...
- Unity UGUI图文混排(七) -- 下划线
之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...
- @property的参数
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51353580 本文出自:[openXu的博客] 参数类别 参数 说明 原子性 atomic ...
- Xcode7.3.1中SKAudioNode在Scene转换后无声的问题
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在新的Xcode中之前可以正常运行的SKAudioNode代码 ...
- GitHub无法访问或访问缓慢解决办法
缘由 由于众所周知的原因,Github最近无法访问或访问很慢.由于Github支持https,因此此次屏蔽Github采用的方法是dns污染,用户访问github会返回一个错误的IPFQ当然是一种解决 ...
- linux crontab定时任务详解
1. 为当前用户创建cron服务: crontab -e 例如 文件内容如下(每隔1分钟执行sql脚本): */1 * * * * mysql -h127.0.0.1 -uroot -proot ...
- Android开发学习之路--NDK、JNI之初体验
好久没有更新博客了,最近一直在看一个仿微信项目,然后看源码并自己实现下,相信经过这个项目可以让自己了解一个项目中的代码以及种种需要注意的事项.不知不觉中博客已经快要40w访问量,而且排名也即将突破30 ...
- 【ShaderToy】基础篇之再谈抗锯齿(antialiasing,AA)
写在前面 在之前的基础篇中,我们讲到了在绘制点线时如何处理边缘的锯齿,也就是使用smoothstep函数.而模糊参数是一些定值,或者是跟屏幕分辨率相关的数值,例如分辨率宽度的5%等等.但这种方法其实是 ...