LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
package leetcode_50; import java.util.ArrayList;
import java.util.List; /***
*
* @author pengfei_zheng
* n对合理括号结果求解问题
*/
public class Solution22 {
public static List<String> generateParenthesis(int n) {
List<String> list = new ArrayList<>();
backtrack(list,"",0,0,n); return list;
}
private static void backtrack(List<String> list,String str, int open, int close, int max) {
if(str.length()==2*max){
list.add(str);
return;
}
if(open<max)
backtrack(list,str+"(",open+1,close,max);
if(close<open)
backtrack(list,str+")",open,close+1,max);
}
public static void main(String[]args){
List<String> list = generateParenthesis(3);
System.out.println(list);
}
}
LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)的更多相关文章
- Leetcode22. Generate Parentheses(生成有效的括号组合)
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- 22. Generate Parentheses产生所有匹配括号的方案
[抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] 22. Generate Parentheses ☆☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- Mac或者linux下登陆到linux上的SFTP
登陆 sftp -i 密钥路径 用户@ip ➜ ~ sftp -i Desktop/aliyun.pem root@39.106.30.1 Connected to 39.106.30.1 上 ...
- [转]oracle awr报告生成和分析
转自:http://blog.csdn.net/cuker919/article/details/8767328 最近由于数据库cpu占用非常高,导致VCS常常自动切换,引起很多问题. 最近学习一下数 ...
- RMAN:简单的duplicate创建新数据库 for 12c+
构建参数文件 *.db_name='test2' ##### 需要注意的地方,和rman的duplicate目标库一致 *.compatible='18.0.0' ##### 关键的地方,每个版本的模 ...
- ubuntu-16.04.1-desktop-amd64.iso:ubuntu-16.04.1-desktop-amd64:安装Oracle11gR2
特点: 使用ubuntu-16.04.1-desktop-amd64.iso liveCD模式 + Casper-rw 本地文件 不降级默认的gcc版本,(liveCD 自带默认为 gcc 5.4): ...
- nodejs的package.json
package.json文件会描述这个NPM包的所有相关信息,包括作者.简介.包依赖.构建等信息,格式是严格的JSON格式 在E:/nodejs/mychat下 执行,npm init 输入yes,就 ...
- MySQL索引优化入门
索引简介 官方定义:索引(Index) 是帮助MySQL高效获取数据的数据结构.大家一定很好奇,索引为什么是一种数据结构,它又是怎么提高查询的速度?我们拿最常用的二叉树来分析索引的工作原理.看下面的图 ...
- pyqt与拉勾网爬虫的结合
人力部需要做互联网金融行业的从业人员薪酬分析,起初说的是写脚本,然后他们自己改.但这样不太好,让人事部来修改py脚本不太好,这需要安装py环境和一些第三方包,万一脚本改来改去弄错了,就运行不起来了. ...
- 8 -- 深入使用Spring -- 4...3 AOP的基本概念
8.4.3 AOP的基本概念 AOP从程序运行角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中各个步骤,希望以更更好的方式来组合业务处理的各个步骤. AOP框架并不与特定的代码耦合 ...
- php 图片上传 500 Internal Server Error 错误
写php简单上传图片时,发现200k的图片上传时报Internal Server Error错误,检查了upload_max_filesize,及其他post_max_size.max_input_t ...
- 采用Post方式提交数据实例
项目目录 一.编写MainActivity.java package com.hyzhou.getdemo; import com.hyzhou.getdemo.service.LoginServer ...