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

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

思路: 回溯法题目。需要用left和right记录当前需要多少个左括号和右括号,注意当left > right时,说明右括号多了,不能在增加右括号,如此只能返回,回溯,寻找新的分支点。

public class Solution {public void dfs(ArrayList<String> result, String s, int left, int right){if(left > right || left < 0 || right < 0){return;}if(left == 0 && right == 0){result.add(s);return;}dfs(result, s+'(', left - 1, right);dfs(result, s+')', left, right - 1);}public ArrayList<String> generateParenthesis(int n) {ArrayList<String> result = new ArrayList<String>();if(n == 0)return result;dfs(result, "", n, n);return result;}
}

[leetcode] Generate Parenthese相关推荐

  1. Leetcode——Generate Parenthese

    2019独角兽企业重金招聘Python工程师标准>>> 问题描述: Given n pairs of parentheses, write a function to generat ...

  2. leetcode : generate parenthese

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

  3. LeetCode——Valid Parenthese

    Leetcode--Valid Parenthese #20 Given a string containing just the characters '(', ')', '{', '}', '[' ...

  4. LeetCode Generate Parentheses

    原题链接在这里:https://leetcode.com/problems/generate-parentheses/ 题目: Given n pairs of parentheses, write ...

  5. [LeetCode] Generate Parentheses

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

  6. [leetcode]Generate Parentheses

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

  7. LeetCode:Generate Parentheses

    题目链接 Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  8. 22. Generate Parenthese

    题目: 解答: 使用穷举形式,对((()))形式的的初始串的第k = n-1个')'做穷举左移,如果左侧也为')'或者已经移动到2 * k - 1的位置时(可以移动的最左侧),k减一,直至所有的右括号 ...

  9. FTPrep, 22 Generate Parenthese

    generate something, 在这里本质上就是生成一个string,按照一定的规则,这个规则就是一棵选择树,不断的向下分支,然后再融合,就是典型的DFS的方法.然后DFS方法就是典型的递归的 ...

最新文章

  1. 读 Linux内核设计与实现 我想到了这些书
  2. 图的dfs非递归_如何理解恶心的递归
  3. ECshop中defined('IN_ECS')的实现原理
  4. 9.proc目录下的文件和目录详解
  5. 专家:未来三年为转云时间窗口
  6. springboot引入外部yml配置文件
  7. Docker学习总结(68)—— Docker 数据卷相关知识总结
  8. 郑继明等《数值分析》习题解答
  9. Voxel-Based Global Illumination
  10. Flink 在众安保险金融业务的应用
  11. 拓端tecdat|R语言中GLM(广义线性模型),非线性和异方差可视化分析
  12. Android包体积优化
  13. 常用三极管的区别 9012 9013 9014 9015 8550 8050
  14. r语言和rstudio_R和RStudio简介
  15. [CV] 基于机器视觉和强化学习的导航
  16. api有哪些 javasocket_简单hello/hi程序、分析及Java Socket API与Linux Socket API对比
  17. pika详解(五)登录认证及connectionParameters
  18. MYSQL查看表结构
  19. 【SCSS】常用的SCSS语法
  20. Oracle文件介质损坏的修复方法

热门文章

  1. 网络安全行业“专业术语”!
  2. 悼念《人月神话》(The Mythical Man-Month)作者 Fred Brooks
  3. java页面字体设置颜色_java 设置字体颜色字体 窗体设置图片背景
  4. 信息管理系统——项目简介和系统架构
  5. bzoj1623【Usaco2008 Open】Cow Cars 奶牛飞车
  6. 2022湖南最新八大员之(安全员)模拟试题题库及答案
  7. codevs——1553 互斥的数
  8. SU2 _斯坦福大学的开源CFD代码
  9. DreamWeaver使用技巧(转)
  10. 【上海科技大学】考研初试复试资料分享