leetcode 022. Generate Parentheses

class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
addingpar(res, "", n, 0);
return res;
}
private:
void addingpar(vector<string> &v, string str, int n, int m){
if(n==0 && m==0) {
v.push_back(str);
return;
}
if(m > 0){ addingpar(v, str+")", n, m-1); }
if(n > 0){ addingpar(v, str+"(", n-1, m+1); }
}
};
class Solution {
public:
vector<string> generateParenthesis(int n) {
if(n==0) return vector<string>(1,"") ;
if(n==1) return vector<string>(1,"()") ;
vector<string> result;
for(int i=0;i!=n;i++)
for(auto inner: generateParenthesis(i))
for(auto outter: generateParenthesis(n-i-1))
result.push_back("("+inner+")"+outter);
return result;
}
};
M Mein-Fuhrer
Reputation: 3
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
string str("(");
result.push_back(str);
vector<int> left({1});
for(int pos = 1;pos < 2*n;++pos) {
int tmp = left.size();
for(int i = 0;i < tmp;++i) {
if(left[i] < n) {
if(pos - left[i] < left[i]) {
result.push_back(result[i] + ')');
left.push_back(left[i]);
}
result[i] += '(';
left[i]++;
}
else {
result[i] += ')';
continue;
}
}
}
return result;
}
};

leetcode022. Generate Parentheses的更多相关文章

  1. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  2. Generate Parentheses

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  3. [LintCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  5. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  7. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  8. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  9. leetcode-algorithms-22 Generate Parentheses

    leetcode-algorithms-22 Generate Parentheses Given n pairs of parentheses, write a function to genera ...

随机推荐

  1. DG_Oracle DataGuard Primary/Standby物理主备节点安装实践(案例)

    2014-09-09 Created By BaoXinjian

  2. NeHe OpenGL教程 第十三课:图像字体

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  3. Shell Python 日期和时间戳的互相转换

    一.初衷: 很多时候,时间的存储都是时间戳格式,如果需要展示就要转化成标准格式日期.也许会需要date和timestamp互转. 二.方法: 1.Shell下对date和timestamp的互转,是通 ...

  4. unsigned char 转字符串:

    通常送显示的都是字符串,对于int long float转字符串有对应的函数,还有sprintf进行格式输出,对于嵌入式和单片机大多都用unsigned char型变量,转字符串需要自己编写函数,需要 ...

  5. php序列化,反序列化

    serialize("数组"); //序列化的函数 序列化示范:serialize(array('1'=>1235622,'2'=>'4142122')); unser ...

  6. cookie和会话状态的工作原理

    一:存在两种类型的cookie: 1>会话cookie (session cookie)        不设置过期时间,则表示这个cookie生命周期为浏览器会话期间,只要关闭浏览器窗口,   ...

  7. eclipse中安装tomcat插件

    一.软件下载 Eclipse3.6 IDE for Java EE Developers: 下载地址:http://eclipse.org/downloads/ Tomcat Eclipse Plug ...

  8. C Primer Plus(第五版)2

    在本章中你将学习下列内容------------------------------------------------------------------1.运算符:= 2.函数:main(),pr ...

  9. cocos2d-x 在xcode IOS模拟器中 开启IOS多点触控

    在初始化代码中,开启当前层接受触摸 this->setTouchEnabled(true); 在AppController.mm文件中,设置开启多点触控 在- (BOOL)application ...

  10. 蓝桥杯---数独(模拟 || dfs)

    [编程题](满分33分) "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数 学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6 ...