[leetcode] Generate Parenthese
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相关推荐
- Leetcode——Generate Parenthese
2019独角兽企业重金招聘Python工程师标准>>> 问题描述: Given n pairs of parentheses, write a function to generat ...
- leetcode : generate parenthese
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode——Valid Parenthese
Leetcode--Valid Parenthese #20 Given a string containing just the characters '(', ')', '{', '}', '[' ...
- LeetCode Generate Parentheses
原题链接在这里:https://leetcode.com/problems/generate-parentheses/ 题目: Given n pairs of parentheses, write ...
- [LeetCode] Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- LeetCode:Generate Parentheses
题目链接 Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- 22. Generate Parenthese
题目: 解答: 使用穷举形式,对((()))形式的的初始串的第k = n-1个')'做穷举左移,如果左侧也为')'或者已经移动到2 * k - 1的位置时(可以移动的最左侧),k减一,直至所有的右括号 ...
- FTPrep, 22 Generate Parenthese
generate something, 在这里本质上就是生成一个string,按照一定的规则,这个规则就是一棵选择树,不断的向下分支,然后再融合,就是典型的DFS的方法.然后DFS方法就是典型的递归的 ...
最新文章
- 读 Linux内核设计与实现 我想到了这些书
- 图的dfs非递归_如何理解恶心的递归
- ECshop中defined('IN_ECS')的实现原理
- 9.proc目录下的文件和目录详解
- 专家:未来三年为转云时间窗口
- springboot引入外部yml配置文件
- Docker学习总结(68)—— Docker 数据卷相关知识总结
- 郑继明等《数值分析》习题解答
- Voxel-Based Global Illumination
- Flink 在众安保险金融业务的应用
- 拓端tecdat|R语言中GLM(广义线性模型),非线性和异方差可视化分析
- Android包体积优化
- 常用三极管的区别 9012 9013 9014 9015 8550 8050
- r语言和rstudio_R和RStudio简介
- [CV] 基于机器视觉和强化学习的导航
- api有哪些 javasocket_简单hello/hi程序、分析及Java Socket API与Linux Socket API对比
- pika详解(五)登录认证及connectionParameters
- MYSQL查看表结构
- 【SCSS】常用的SCSS语法
- Oracle文件介质损坏的修复方法