leetcode第21题--Generate Parentheses
problem:
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
一想到要用递归来解决的就有点头疼,因为还没有好好系统的回顾一下递归,总觉得判断什么时候返回很困难。这个是这样的,用l和r分别记录剩下的左括号和右括号。当l和r都为零的时候就是输出了一种正确的可能了。前提是任何时刻,剩下的右括号要比左括号多或者相等,否则返回。其次,r和l都为零的时候,就把temp str记录下来。代码和部分注释如下:
class Solution {
private:
void subGe(int l, int r, string t, vector<string> &ans)
{
if(l > r) // 左代表‘(’右代表‘)’,剩下的左括号要比右括号少才合法
return ;
if (l == && r == )
{ans.push_back(t);return;} // 这是t已经是2n个字符了
if(l > )
subGe(l - , r, t + "(", ans); // 输出一个(,则左括号减一
if(r > )
subGe(l, r - , t + ")", ans);
}
public:
vector<string> generateParenthesis(int n)
{
vector<string> ans;
ans.clear();
if (n <= )
return ans;
subGe(n, n, "", ans);// 初始时,左右括号都是n个
return ans;
}
};
leetcode第21题--Generate Parentheses的更多相关文章
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- LeetCode 笔记系列五 Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- LeetCode(22)Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- leetcode第20题--Valid Parentheses
Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- leetcode个人题解——#22 Generate Parentheses
思路: 递归解决,如果左括号个数小于右括号或者左括号数小于总括号对数,则生成一个左括号,如果左括号数大于右括号,生成一个右括号. class Solution { public: vector< ...
- LeetCode第21题:合并两个有序链表
题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1- ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- 【LeetCode算法-21】Merge Two Sorted Lists
LeetCode第21题 Merge two sorted linked lists and return it as a new list. The new list should be made ...
- 乘风破浪:LeetCode真题_022_Generate Parentheses
乘风破浪:LeetCode真题_022_Generate Parentheses 一.前言 关于括号的题目,我们已经遇到过了验证正确性的题目,现在让我们生成合法的括号列表,怎么办呢?想来想去还是递归比 ...
随机推荐
- error LNK2019: 解析的外部符号 __imp__DispatchMessageW@4,在函数的符号 _WinMain@16 据引述
错误: 1>WinMain.obj : error LNK2019: 解析的外部符号 __imp__DispatchMessageW@4,在函数的符号 _WinMain@16 据引述 1> ...
- SDUT 1265-马停下过河卒(DFS)
马拦过河卒 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit3000ms Memory Limit 65536K ...
- MessageFormat类别:快速格式化字符串
MessageFormat 获取一组对象,格这些对象的类型,然后格串类型插入的地方的图案. 第一个样例使用静态的方法 MessageFormat.format.它在内部创建一个仅仅使用一次的 Mess ...
- HDU1312 Red and Black 解读
递归搜索方法标题,采用递归搜索方法,但是,如果没有迭代计算的真正的政党格. 我们的想法是: 1 每一个搜索为党格要改变电流方向格的值至 '*',或任何其他非'.'的值,代表方格了 2 递归的时候不回复 ...
- linux VIM基本命令
linux VIM命令: vim 在命令行中输入vim,进入vim编辑器 Esc 退出i(插入)命令进行其他命令使用 :sh 进入shell命令行,运行完命令后ctrl+d退出又一次进入vim编辑继续 ...
- SQLServer表变量对IO及内存影响测试
原文:SQLServer表变量对IO及内存影响测试 1. 测试创建表变量对IO的影响 测试创建表变量前后,tempdb的空间大小,目前使用sp_spaceused得到大小,也可以使用视图sys.dm_ ...
- [.Net Tools] 超強大的封裝工具 Advanced Installer
原文:[.Net Tools] 超强大的封装工具Advanced Installer 日前在网路上晃到这家公司的产品http://www.advancedinstaller.com/,就直接下载并且安 ...
- iframe参数
iframe参数: <iframe src="test.jsp" width="100″ height="50″ frameborder="no ...
- DevExpress XtraReports 入门一 创建 Hello World 报表
原文:DevExpress XtraReports 入门一 创建 Hello World 报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助更 ...
- hdu 亲和串(kmp)
Problem Description 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现 ...