本题分别采用两种方法:暴力法(set+vector) 以及 dfs(vector)

vector<string> generateParenthesis(int n) {set<string> resSet;//利用set的去重 将所有的可能放入set中 然后转换成vectorvector<string> res;//boundary conditionif (n == 0)return res;resSet.insert("()");if (n == 1){res.push_back("()");return res;}// !recursionfor (int i = 2; i <= n; i++){set<string> resTemp = resSet;resSet.clear();for (auto s : resTemp){//brackets is between the s  挨个插括号 每个位置都插一遍for (int j = 0; j < s.size(); j++){string left = s.substr(0, j);string right = s.substr(j);resSet.insert(left + "()" + right);}//brackets is out of the sresSet.insert("(" + s + ")");}}//take the string in the vectorfor (auto s : resSet){res.push_back(s);}return res;}


明显的比较出DFS在时间这方面是多么高效

void DFS(vector<string> &vs, string curstr, int left, int right) //remain left '(' and right ')'
{//boundary conditionif (left > right)//左括号剩余数量不能多余右括号return;if (left < 0 || right < 0)return;if (left == 0 && right == 0){vs.push_back(curstr);return;}if (left > 0) //if the left is remained ,continue traverse DFS(vs, curstr + '(', left - 1, right);if (right > 0)//if the right is remained ,continue traverseDFS(vs, curstr + ')', left, right - 1);
}vector<string> generateParenthesis(int n)
{vector<string> res;//boundary conditionif (n == 0)return res;if (n == 1){res.push_back("()");return res;}// DFSDFS(res, "", n, n);//the number of '(' and ')' are both nreturn res;
}

Leetcode每日一题:22.generate-parentheses(括号生成)相关推荐

  1. LeetCode每日一题(22年1月7日-1月16日)

    目录(每日一题) 1614. 括号的最大嵌套深度 89. 格雷编码 1629. 按键持续时间最长的键 306. 累加数 1036. 逃离大迷宫 334. 递增的三元子序列 747. 至少是其他数字两倍 ...

  2. 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)

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

  3. LeetCode每日一题——1758. 生成交替二进制字符串的最少操作数

    LeetCode每日一题系列 题目:1758. 生成交替二进制字符串的最少操作数 难度:简单 文章目录 LeetCode每日一题系列 题目 示例 思路 题解 题目 给你一个仅由字符 '0' 和 '1' ...

  4. [leetcode] 22. Generate Parentheses

    题目大意 https://leetcode.com/problems/generate-parentheses/description/ 22. Generate Parentheses Given ...

  5. leetcode每日刷题计划-简单篇day8

    leetcode每日刷题计划-简单篇day8 今天是纠结要不要新买手机的一天QAQ想了想还是算了吧,等自己赚钱买,加油 Num 70 爬楼梯 Climbing Stairs class Solutio ...

  6. 【Leetcode每日一题】118. 杨辉三角(水题)

    Leetcode每日一题 题目链接: 118. 杨辉三角 难度: 简单 解题思路: 无.见代码. 题解: class Solution:def generate(self, numRows: int) ...

  7. Leetcode每日一题:171.excel-sheet-column-number(Excel表列序号)

    思路:就是168题的反命题,进制的方式完美解决: Leetcode每日一题:168.excel-sheet-column-title(Excel表名称) class Solution {public: ...

  8. 【LeetCode每日一题】1723. 完成所有工作的最短时间

    [LeetCode每日一题]1723. 完成所有工作的最短时间 [1] 1723. 完成所有工作的最短时间 [2] 473. 火柴拼正方形 [1] 1723. 完成所有工作的最短时间 题目: 给你一个 ...

  9. leetcode每日一题--雀巢原理;抽屉算法;Quorum机制;分布式应用

    leetcode每日一题 539. 最小时间差 示例 1: 输入:timePoints = ["23:59","00:00"] 输出:1 示例 2: 输入:ti ...

最新文章

  1. 老年手机英文改中文_这些手游大作你都玩了吗?手机游戏推荐
  2. 读取资源文件的工具.
  3. 谷歌大脑科学家亲解 LSTM:一个关于“遗忘”与“记忆”的故事
  4. MySQL的空值查询
  5. st edmunds和emmanuel college
  6. linux学习项目,[ Linux运维学习 ] 路径及实战项目合集
  7. java set 对象去重_JAVA对象去重
  8. 解决Ubuntu Chrome浏览器很卡不响应的问题
  9. ICML 2019 | 强化学习用于推荐系统,蚂蚁金服提出生成对抗用户模型(附论文下载链接)...
  10. Android开发学习路线图
  11. 计算机办公自动化试题及答案,计算机等级考试,办公自动化考试试题(三)
  12. 黑客窃取企业数据的后门程序技巧
  13. 罗技键盘的insert键需要按住Fn才能生效?教你修改!
  14. Manjaro Linux 双显卡安装步骤及独立显卡运行游戏(Nvidia GeForce GTX 980m)
  15. Kibana7.9.2设置elasticsearch索引过期时间,到期自动删除
  16. Integer类型的比较
  17. Revit二次开发从入门到精通学习之路, (含Revit二次开发教程下载)
  18. text/plain
  19. 一个简单的用户登录界面
  20. 信息量,熵,互信息,信道容量等相关概念

热门文章

  1. 微信小程序滚动条返回顶部
  2. Python基础十四——内置函数和匿名函数
  3. nodejs实战《一起学 Node.js》 使用 Express + MongoDB 搭建多人博客
  4. 数组引用--冒泡排序 数组倒置为例
  5. c#数据类型的值传递和引用传递--基础拾遗
  6. APP动态界面设计使用的利与弊
  7. 枚举数据库中所有表的列名(转)
  8. CDC::GetDeviceCaps()物理长度与屏幕像素间的转换
  9. HTTP安全随想,第一指导原则:不要相信任何用户的输入[]
  10. 设计模式学习笔记(十八:模板方法模式)