博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode从零单排】No22.Generate Parentheses
阅读量:7047 次
发布时间:2019-06-28

本文共 1159 字,大约阅读时间需要 3 分钟。

题目

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:

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

代码

For 2, it should place one "()" and add another one insert it but none tail it,

'(' f(1) ')' f(0)

or add none insert it but tail it by another one,

'(' f(0) ')' f(1)

Thus for n, we can insert f(i) and tail f(j) and i+j=n-1,

'(' f(i) ')' f(j)

public List
generateParenthesis(int n) { List
result = new ArrayList
(); if (n == 0) { result.add(""); } else { for (int i = n - 1; i >= 0; i--) { List
insertSub = generateParenthesis(i); List
tailSub = generateParenthesis(n - 1 - i); for (String insert : insertSub) { for (String tail : tailSub) { result.add("(" + insert + ")" + tail); } } } } return result;}

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:

******************************************/

你可能感兴趣的文章
《vSphere性能设计:性能密集场景下CPU、内存、存储及网络的最佳设计实践》一3.3.4 定义IOmeter的工作负载和配置...
查看>>
基于数加,全民众筹平台开启大数据智能时代 | 上海云栖
查看>>
详解百度应用的 WormHole 后门
查看>>
《UNIX/Linux 系统管理技术手册(第四版)》——1.2 读者的知识背景
查看>>
《算法基础》——1.3 伪代码
查看>>
第十二天:规划成本管理,成本类型, 资产折旧;和 估算成本,估算成本知识点提示...
查看>>
《UNIX编程环境》——5.8 news命令:社团服务信息
查看>>
《编写高质量Python代码的59个有效方法》——第17条:在参数上面迭代时,要多加小心...
查看>>
《算法基础:打开算法之门》一第3章 排序算法和查找算法
查看>>
《Linux嵌入式实时应用开发实战(原书第3版)》——3.8 获得帮助
查看>>
2015年,Facebook 排名前 5 的开源项目
查看>>
《精通Nginx》一导读
查看>>
甲骨文 VS. Google——一场将会改变整个软件行业的官司是如何发生的?
查看>>
【又一重磅】再获翻译授权,斯坦福CS231N深度学习与计算机视觉
查看>>
《Node.js区块链开发》导读
查看>>
《Ansible权威指南》一1.2 Ansible发展史
查看>>
泛型中? super T和? extends T的区别
查看>>
《Spark大数据分析:核心概念、技术及实践》一3.9 共享变量
查看>>
理解RESTful架构
查看>>
【问答集锦】人工智能/机器学习技术在电商场景下的应用
查看>>