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 ...
随机推荐
- Embedded System.
Soc ( System on Chip) Soc is an integrated circuit (IC) that integrates all components of a computer ...
- 手势识别(一)--手势基本概念和ChaLearn Gesture Challenge
以下转自: http://blog.csdn.net/qq1175421841/article/details/50312565 像点击(clicks)是GUI平台的核心,轻点(taps)是触摸平台的 ...
- RMAN_学习实验2_RMAN Duplicate复制数据库过程(案例)
待整理 对于基于生产环境下的数据库的版本升级或者测试新的应用程序的性能及其影响,备份恢复等等,我们可以采取从生产环境以克隆的方式将其克隆到本地而不影响生产数据库的正常使用.实现这个功能我们可以借助rm ...
- linux下sublime配置c++11编译环境
Tools->Build System->New Build System { "cmd": ["g++", "-std=c++11&qu ...
- codeforces 557 D. Vitaly and Cycle 组合数学 + 判断二分图
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input sta ...
- contentProvider 内容提供者
http://blog.csdn.net/woshixuye/article/details/8280879 实例代码当数据需要在应用程序间共享时,我们就可以利用ContentProvider为数据定 ...
- 第3章 System V IPC
3.1 概述 System V IPC 包含:System V消息队列.System V信号量.System V共享内存. 3.2 key_t 键和 ftok函数 这三种类型的System V IPC ...
- 第2章 Posix IPC
2.1 概述 Poxix IPC包含:Posix消息队列.Posix信号量.Posix共享内存 2.2 IPC名字 Posix 消息队列.Posix信号量.Posix共享内存这三种Posix IPC都 ...
- [ActionScript 3.0] AS3动态改变注册点
package { import flash.display.DisplayObject; import flash.display.DisplayObjectContainer; import fl ...
- angularJs编写多指令的情况
本实例主要展示controller和link参数的使用.以及多个指令同时作用的情况. <!DOCTYPE html> <html ng-app="myModule" ...