leetcode022. Generate Parentheses
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的更多相关文章
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- leetcode-algorithms-22 Generate Parentheses
leetcode-algorithms-22 Generate Parentheses Given n pairs of parentheses, write a function to genera ...
随机推荐
- SparkStreaming入门及例子
看书大概了解了下Streaming的原理,但是木有动过手啊...万事开头难啊,一个wordcount 2小时怎么都运行不出结果.是我太蠢了,好了言归正传. SparkStreaming是一个批处理的流 ...
- Hbase的安装(hadoop-2.6.0,hbase1.0)
Hbase的安装相对很简单啊...只要你装了Hadoop 装Hbase就是分分钟的事 如果要装hadoop集群的话 hadoop分类的集群安装好了,如果已经装好单机版~ 那就再配置如下就好~ 一.vi ...
- ADF_Starting系列5_使用ADF开发富Web应用程序之维护User Interface(Part2)
2014-05-05 Created By BaoXinjian
- PO_PO系列 - 收货管理分析(案例)
2014-07-01 Created By BaoXinjian
- python(19)编码问题
转载链接:http://blog.csdn.net/lxdcyh/article/details/4018054 python的编码问题,一直都让人很头疼,今天就整理下python的编码知识. 首先: ...
- cotex_m3内核提供的ITM串口打印调试
cotex_m3内核的ARM提供了ITM串口打印观测的功能,可以不用ARM单片机自己的串口就可在开发时候串口打印调试.节约了宝贵的内部资源,同时也为调试提供了方便.使用方法如下: 1 将下面的SWO_ ...
- JAVA 创建类,使用类
一.创建类: Test.java //定义类 public class Test{ //属性 String name; String gender; int age; //方法,无参无返回 publi ...
- git fetch和git pull(转载)
From:http://www.tech126.com/git-fetch-pull/ Git中从远程的分支获取最新的版本到本地有这样2个命令: 1. git fetch:相当于是从远程获取最新版本到 ...
- iPhone开发之UIScrollView初步
来源:http://blog.csdn.net/htttw/article/details/7891396 iPhone开发之UIScrollView初步 今天我们初步介绍以一下iPhone开发中的U ...
- sqlite 时间排序
select * from tb_QuantifyResult where iSamplingOrCalibration = 1 and cComponentName <> ' + Quo ...