leetcode — generate-parentheses
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/generate-parentheses/
*
* Created by lverpeng on 2017/7/11.
*
* 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:
*
* "((()))", "(()())", "(())()", "()(())", "()()()"
*
*/
public class GenerateParentheses {
/**
* 生成的括号个数是2n个(包括左括号和右括号),而且1-2n范围内左括号的个数一定大于右括号的个数
*
* @param n
* @return
*/
public List<String> generate (int n) {
List<String> list = new ArrayList<String>();
generate(n, n, list, "");
return list;
}
/**
* 使用深度优先算法
*
* @param left 左括号的个数
* @param right 右括号的个数
* @param result 最终字符串的保存在这里
* @param str
*/
private void generate (int left, int right, List<String> result, String str) {
if (left == 0 && right == 0) {
result.add(str);
}
if (left > 0) {
generate(left - 1, right, result, str + "(");
}
// 维护括号配对的规则,先有左括号,才能有右括号
if (left < right && right > 0) {
generate(left, right - 1, result, str + ")");
}
}
public static void main(String[] args) {
GenerateParentheses generateParentheses = new GenerateParentheses();
List<String> list = generateParentheses.generate(2);
System.out.println(Arrays.toString(list.toArray(new String[list.size()])));
}
}
leetcode — generate-parentheses的更多相关文章
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode Generate Parentheses (DFS)
题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode——Generate Parentheses
Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...
- LeetCode: Generate Parentheses [021]
[称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- LeetCode Generate Parentheses 构造括号串(DFS简单题)
题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...
- leetcode Generate Parentheses python
# 解题思路:列举出所有合法的括号匹配,使用dfs.如果左括号的数量大于右括号的数量的话,就不能产生合法的括号匹配class Solution(object): def generateParenth ...
- 并没有看起来那么简单leetcode Generate Parentheses
问题解法参考 它给出了这个问题的探讨. 超时的代码: 这个当n等于7时,已经要很长时间出结果了.这个算法的复杂度是O(n^2). #include<iostream> #include&l ...
随机推荐
- [Selenium] 在Chrome的开发者工具中验证检查XPath/CSS selectors
Evaluate and validate XPath/CSS selectors in Chrome Developer Tools Method 1 : From Elements panel U ...
- C++ 提取网页内容系列之三
标 题: C++ 提取网页内容系列作 者: itdef链 接: http://www.cnblogs.com/itdef/p/4171659.html 欢迎转帖 请保持文本完整并注明出处 这次继续下载 ...
- asp.net core2.0中网站发布的时候,怎么样才配置才可以使视图文件不被打包进去?
默认设置可真是坑~~ https://q.cnblogs.com/q/99680/
- Axure RP Extension for Chrome 插件安装
描述 我的chmod浏览器上不去谷歌商店,我用的是蓝灯,登上商店后搜索Axure RP Extension for Chrome,下载安装,完成后进入这个插件的详细信息: 使用 打开用axure生成的 ...
- Python Day 14 迭代器、for循环原理、枚举、生成器
阅读内容 内容回顾 带参装饰器和wraps用法 迭代器知识引入 可迭代对象 迭代器对象 for循环迭代器 枚举对象 生成器 ##内容回顾 函数的嵌套定义:在函数内部定义另一 ...
- zabbix items
zabbixversion:3.2.8 在添加zabbix items的时候,注意store value的设置非常实用,有三种选择: As is:d对接受到的结果不做任何处理 Delta(speed ...
- navicat for mysql安装与破解
Windows系统的电脑 navicat for mysql 的安装软件以及破解包 方法/步骤 从网上下载需要用到的navicat for mysql 的安装软件以及破解包. 双击navi ...
- Docker基础-使用Dockerfile创建镜像
1.基本结构 Dockerfile由一行行命令语句组成,并支持以#开头的注释行.例如: # This dockerfile uses the ubuntu image # VERSION 2 - ED ...
- 连接SSH服务器的脚本,自动发送用户名和密码
利用expect 自动输入用户名和密码 脚本如下 #!/usr/bin/expect # connect ssh server set timeout 30 spawn ssh -l user_nam ...
- 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, VerticalAlignment
[源码下载] 背水一战 Windows 10 (75) - 控件(控件基类): FrameworkElement - 基础知识, 相关事件, HorizontalAlignment, Vertical ...