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. ahk鼠标连击工具

    ;x = 0开始点击,x = 1暂停点击 ^::ck_start() ^::ck_end() ck_start() { x = { Click } } ck_end() { x = else x = ...

  2. PLSQL_批量压缩表Table Compress(案例)

    2015-04-01 Created By BaoXinjian

  3. SteamVR Unity工具包(VRTK)之控制器交互

    可交互对象(VRTK_InteractableObject) 可交互对象脚本被添加到需要用(如控制器)来交互的任何游戏对象上.   可用脚本参数如下   Touch Interactions 触摸交互 ...

  4. 转-Android 之 使用File类在SD卡中读取数据文件

    如果需要在程序中使用sdcard进行数据的存储,那么需要在AndroidMainfset.xml文件中 进行权限的配置: Java代码:   <!-- 在sd中创建和删除文件的权限 --> ...

  5. gerrit 配置 apache2 反向代理(转载)

    Apache 2 Configuration To run Gerrit behind an Apache server using mod_proxy, enable the necessary A ...

  6. 检测是否安装有sim卡

    ios7测试ok [CTSIMSupportGetSIMStatus() isEqualToString:kCTSIMSupportSIMStatusNotInserted]可以判断是否插入了sim卡 ...

  7. Mybatis where 1=1 和 <where>标签

    <select id="selSampleListByIDX4" resultMap="BaseResultMap" parameterType=&quo ...

  8. 在ubuntu下安装phpmyadmin 出现404错误

    在ubuntu下安转phpmyadmin 只要一条命令: Sudo apt-get install phpmyadmin 安装完成后,在浏览器里输入http://localhost/phpmyadmi ...

  9. 如何在XAMPP中设置多个网站

    xampp 是一个非常方便的本地 apache + php + mysql 的调试环境,在本地安装测试 WordPress 等各种博客.论坛程序非常方便.今天我们来给大家介绍一下,如何使用 XAMPP ...

  10. [ActionScript 3.0] AS3动态改变注册点

    package { import flash.display.DisplayObject; import flash.display.DisplayObjectContainer; import fl ...