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. 最大期望算法 Expectation Maximization概念

    在统计计算中,最大期望(EM,Expectation–Maximization)算法是在概率(probabilistic)模型中寻找参数最大似然估计的算法,其中概率模型依赖于无法观测的隐藏变量(Lat ...

  2. [ Iptables ] Linux开启防火墙,切记仔细确定每个端口

    一.缘由: 祸起Redis未授权访问漏洞被利用,删除了服务器的所有账号,导致无法登陆:这才不得不把开启防火墙提上日程.再次在开启防火墙过程中,一刀切造成了一些前段进程的端口被封,甚是后悔! 二.解决办 ...

  3. [复变函数]第17堂课 5 解析函数的 Laurent 展式与孤立奇点 5. 1 解析函数的 Laurent 展式

    0.  引言 (1)  $f$ 在 $|z|<R$ 内解析 $\dps{\ra f(z)=\sum_{n=0}^\infty c_nz^n}$ (Taylor 级数). (2)  $f$ 在 $ ...

  4. 常用到的Mysql语句

    经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...

  5. jquery 源码剖析1

    ()();   闭包,让声明的变量变成局部变量,使外部无法访问,防止和其他代码冲突,互不影响. (function(){ })();              和一般执行方法一样的. jQuery=f ...

  6. ConcurrentHashMap使用要点

    ConcurrentHashMap的简要总结: 1.public V get(Object key)不涉及到锁,也就是说获得对象时没有使用锁: 2.put.remove方法要使用锁,但并不一定有锁争用 ...

  7. iconv 批量修改文件编码

    iconv_shell.sh #!/bin/bash "];then echo "Usage: `basename $0` dir filter" exit fi dir ...

  8. 91、sendToTarget与sendMessage

    Message msg = handler.obtainMessage();               msg.arg1 = i;               msg.sendToTarget(); ...

  9. java socket通讯(一) 入门示例

    一.入门 要想学习socket通讯,首先得知道tcp/ip和udp连接,具体可参考浅谈TCP/IP 和 UDP的区别 二.示例 首先新建了一个java工程,包括两个部分,客户端SocketClient ...

  10. gomoblie flappy 源码分析:图片素材和大小的处理

    flappy的源码可以在 https://github.com/golang/mobile 看到.具体在 https://github.com/golang/mobile/tree/master/ex ...