Generate Parentheses 解答
Question
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:
"((()))", "(()())", "(())()", "()(())", "()()()
Solution 1
Two conditions to check:
if remained left '(' nums < remained right ')', for next char, we can put '(' or ')'.
if remained left '(' nums = remained right ')', for next char, we can only put '('.
Draw out the solution tree, and do DFS.
public class Solution {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<String>();
List<Character> list = new ArrayList<Character>();
dfs(n, n, list, result);
return result;
} private void dfs(int leftRemain, int rightRemain, List<Character> list, List<String> result) {
if (leftRemain < 0 || rightRemain < 0 || leftRemain > rightRemain)
return; if (leftRemain == 0 && rightRemain == 0) {
int size = list.size();
StringBuilder sb = new StringBuilder(size);
for (char tmpChar : list)
sb.append(tmpChar);
result.add(sb.toString());
return;
} if (leftRemain == rightRemain) {
list.add('(');
dfs(leftRemain - 1, rightRemain, list, result);
list.remove(list.size() - 1);
} else {
list.add(')');
dfs(leftRemain, rightRemain - 1, list, result);
list.remove(list.size() - 1);
list.add('(');
dfs(leftRemain - 1, rightRemain, list, result);
list.remove(list.size() - 1);
}
}
}
Solution 2
A much simpler solution.
public class Solution {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<String>();
List<Character> list = new ArrayList<Character>();
dfs(n, n, "", result);
return result;
} private void dfs(int leftRemain, int rightRemain, String prefix, List<String> result) {
if (leftRemain < 0 || rightRemain < 0 || leftRemain > rightRemain)
return;
if (leftRemain == 0 && rightRemain == 0) {
result.add(new String(prefix));
return;
}
if (leftRemain > 0)
dfs(leftRemain - 1, rightRemain, prefix + "(", result);
if (rightRemain > 0)
dfs(leftRemain, rightRemain - 1, prefix + ")", result);
}
}
Generate Parentheses 解答的更多相关文章
- Generate Parentheses - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Generate Parentheses - LeetCode 注意点 解法 解法一:递归.当left>right的时候返回(为了防止出现 )( ) ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode022. Generate Parentheses
leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
随机推荐
- 关于Yeoman使用的总结
Yeoman由三部分组成 Yo 用于项目构建. Grunt 用于项目管理,任务制定. Bower 用于项目依赖管理. 经过一段时间的使用,对这些东西有了一些个人总结: 总体上说这些内容学习曲线略高,不 ...
- postGreSQL数据库部署及简单使用
1,deployByRuiyIns rpm -ivh http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-centos94-9.4-1.no ...
- [置顶] java ant 配置及构建项目
Ant是一种基于Java的构建工具.Ant文件是配置构建目标过程的XML文件,也称为Ant脚本. (因为对这个不是很了解,所以用词方面可能于个人的理解有偏差 ...
- [bzoj1003][ZJOI2006][物流运输] (最短路+dp)
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- c++之 变量
变量的基本操作 变量就是一个可以变化的量,变量由变量类型.变量名.初始值(可选)组成,例如: int abc = 10; 变量类型:int 变量名:abc 初始值:10 // 该值为可选项,在创建变量 ...
- 地下迷宫(bfs输出路径)
题解:开一个pre数组用编号代替当前位置,编号用结构题另存,其实也可以i*m+j来代替,我写的有点麻烦了; 代码: #include <iostream> #include <cst ...
- [RxJS] Reactive Programming - Why choose RxJS?
RxJS is super when dealing with the dynamic value. Let's see an example which not using RxJS: var a ...
- LR脚本自定义显示Controller虚拟用户状态
在场景监控的过程中,想知道场景运行时Vusers的运行状态以及每一个Vuser虚拟用户在本次场景运行的过程共迭代了多少次,那么就需要在VuGen脚本中自定义显示虚拟用户状态信息. 代码如下: stat ...
- Oracle:grouping和rollup
Oracle grouping和rollup简单测试 SQL,,,) group by department_id order by department_id; DEPARTMENT_ID SUM( ...
- CSS基础笔记
之前没有开通好博客,笔记都记录在有道云,今天全部转过来!!! 1.当同一个html元素不止一个样式定义时,内联样式(在html元素内部)拥有最高的优先权:其他如内部样式表(位于<head> ...