原文链接:https://leetcode.com/problems/generate-parentheses

给出数字n,求n对括号组成的合法字符串。

刚做出来,敲完代码,修改了一次,然后提交,accepted ~ 还是小有成就感的。。菜鸟...

先记录下自己的笨方法。再研究更高妙的方法。

刚看到题也是一头雾水。没法深入思考,感觉就是不断的罗列,被题目吓到了。

仔细观察,合法字符串的第一个字母肯定是( ,最后一个肯定是)。

对于合法字符串的每一位,必定有 左括号的个数 >= 右括号的个数。每一位都必须满足。

想法就是,从第一位开始向最后一位扩充,直到填满2*n位。

当填第i位的时候,考察前面左括号的个数left,和右括号的个数right。有下面三种情况:

1、left == right 左右括号相等了,这时候只能填左括号。

2、left == n , right < n  左括号已经有n个了,满了,只能填右括号了。

3、left < n && left > right  左括号还没满,且比右括号多。这时候,可以填两种。

本来想递推算,但是后来习惯性改成了循环。

用C太麻烦了!

C++的STL,边搜语法边照着敲的。肯定用的很蹩脚。见谅。

class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<int>left;  //第i个字符串左括号的个数
vector<int>right;  //第i个字符串右括号个数
vector<string> ans;
int i,j,k;
string tmp("("); ans.push_back(tmp);  //第一个字符串就是"("
left.push_back();  //第一个字符串的左括号有一个
right.push_back();  //右括号0。 for(i = ; i<*n; i++)  //开始添加第i位。
{
int len = ans.size();
for(j = ;j < len; j++)  //遍历每个字符串
{
if(left[j] == right[j])
{
ans[j].append(,'(');
left[j] ++;
}
else if(left[j]>right[j] && left[j] < n)
{
string tmp(ans[j]);
tmp.append(,'(');
ans.push_back(tmp);
left.push_back(left[j]+);
right.push_back(right[j]); ans[j].append(,')');
right[j]++;
}
else if(left[j] == n)
{
ans[j].append(,')');
right[j]++;
}
}
}
return ans;
}
};

leetcode题解 Generate Parentheses的更多相关文章

  1. [LeetCode 题解]: Generate Parentheses

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

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

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

  3. LeetCode 022 Generate Parentheses

    题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...

  4. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  5. leetcode之 Generate Parentheses

    题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...

  6. [LeetCode] 22. Generate Parentheses 生成括号

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

  7. LeetCode 22. Generate Parentheses

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

  8. 【leetcode】Generate Parentheses

    题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  9. 【leetcode】 Generate Parentheses (middle)☆

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

随机推荐

  1. 数组连接a.concat(b),b作为一个整体

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. Django的模板层简介

    Django的模板层 如果我们想要利用视图函数返回一个页面,一种比较简单的方式是利用HttpResponse()方法返回一个含有html内容的字符串: def current_datetime(req ...

  3. python浅copy和深copy

    import   copy person =["name",[count,3000]] husband=copy.copy(person) wife=copy.copy(perso ...

  4. 搭建postgresql集群的问题汇总

    问题一:如何配置pg远程访问          修改postgresql.conf-->listen_addresses = '*'          修改pg_hba.conf-->添加 ...

  5. Centos7修改系统时区timezone

    第一步:查询服务器时间 [root@localhost ~]# timedatectl Local time: Sat 2018-03-31 01:11:46 UTC Universal time: ...

  6. Redis进阶实践之十四 Redis-cli命令行工具使用详解

    转载来源:http://www.cnblogs.com/PatrickLiu/p/8508975.html 一.介绍 redis学了有一段时间了,以前都是看视频,看教程,很少看官方的东西.现在redi ...

  7. dict函数

    增 fromkeys(iterable, value) 用可迭代对象生成键,创建默认值相同的字典(value默认None) 删 pop(k) 通过k来删除字典元素, 找不到就会报错, 返回被删除字典元 ...

  8. html代码段

    添加icon<link rel="shortcut icon" href="img/100du.ico"/>

  9. 第7章 网络层协议(2)_ICMP协议

    2. ICMP协议 2.1 ICMP报文(Internet Control Message Protocol)的类型 报文类型 类型值 代码 描述 请求报文 8 0 请求回显报文 响应报文 0 0 回 ...

  10. pthread线程特定数据

    举个栗子 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/t ...