原文链接: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. 函数的进阶(动态参数,命名空间和作用域,函数的嵌套,gloabal和nonlocal关键字)

    1. 函数参数--动态传参 昨天已经解过了传参, 如果需要给一个函数传参,而参数又是不确定的. 或者我们给一个函数传很多参数, 我的形参就要写很多, 写起来就很麻烦, 这时我们可以考虑使用动态参数.形 ...

  2. 使用 keepalived 设置虚拟 IP 环境(转载)

    使用 keepalived 设置虚拟 IP 环境 原文地址:https://blog.csdn.net/kongxx/article/details/73173762 使用 keepalived 设置 ...

  3. Blob下载文件 & 模拟滚动条实现

    1.vue切换路由视图时,事件钩子顺序是 当前模块create-->上一个模块beforeDestroy-->当前模块mounted 因此注册全局事件(比如给window注册事件)应放在m ...

  4. windows 日志解决方法

    1.sql server 2012 报错 MSSQLSERVER 服务无法使用当前配置的密码以 .\MSSQL_SF_A9JGSK 身份登录,错误原因如下: 此帐户的密码已过期. 要确保服务配置正确, ...

  5. DNS污染

    参考链接:http://blog.csdn.net/charleslei/article/details/50117761 DNS污染: DNS污染,又称域名服务器缓存污染(DNS cache pol ...

  6. js之DOM

    DOM对象 什么是HTML  DOM HTML  Document Object Model(文档对象模型) HTML DOM 定义了访问和操作HTML文档的标准方法 HTML DOM 把 HTML ...

  7. CS229 6.3 Neurons Networks Gradient Checking

    BP算法很难调试,一般情况下会隐隐存在一些小问题,比如(off-by-one error),即只有部分层的权重得到训练,或者忘记计算bais unit,这虽然会得到一个正确的结果,但效果差于准确BP得 ...

  8. 7 家 IT 厂商 6394.5 万元中标天津公安云项目(虚拟化、数据库、软件开发)

    http://mp.weixin.qq.com/s/kjum54HJorGTPtZiM-HE1g 天津市公安局云计算平台项目分为:大数据部分.虚拟化部分.数据库部分,软件开发部分,预算分别为:2350 ...

  9. Android仿淘宝头条滚动广告条

    之前我使用TextView+Handler+动画,实现了一个简单的仿淘宝广告条的滚动,https://download.csdn.net/download/qq_35605213/9660825: 无 ...

  10. vue 整合element-ui

    本文主要介绍如何在vue框架中结合elementUI. 本文主要参考: http://element-cn.eleme.io/#/zh-CN/component/quickstart   1.阅读本文 ...