LeetCode 10.9 Generate Parentheses
2016-04-27 10:08
253 查看
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:
"((()))", "(()())", "(())()", "()(())", "()()()"import java.util.Iterator;import java.util.Stack;public class T5 {public static void main(String[] args) { T5 t=new T5(6); t.DFS(0); t.print(); } Stack<int [] > ans=new Stack<>(); int n; int[] f; int pre=-1; T5(int _n){ n=_n; f=new int[n]; } void DFS(int k){ if(k==n){ int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=f[i]; } ans.add(arr); }else{ for(int i=pre==-1?0:pre;i<n;i++){ if(k<=i){ pre=i; f[k]=i+1; DFS(k+1); } } } } void print(){ int num=1; Iterator<int[]> it = ans.iterator(); while(it.hasNext()){ System.out.println("======"+num+"====="); int []temp=it.next(); String s=""; int l=n; while(l-->0){ s+="("; } for(int i=temp.length-1;i>=0;i--){ s=s.substring(0, temp[i])+")"+s.substring(temp[i]); } System.out.println(s); num++; } }}
相关文章推荐
- alarm 信号
- java异常体系
- Android 图片填充动画
- Kafka消费者Consumer常用配置
- 缺8数
- ubuntu 14.04 安装boost
- lkmusic项目WebAudio版本支持音乐可视化 已更新至github 欢迎下载
- [快速数论变换 NTT]
- hive premanent udf 发布...
- oracle汉字转拼音(获得全拼/拼音首字母/拼音截取等)
- 打开或关闭Activity的动画
- CentOS 6 下安装php5.5.34的 ffmpeg 扩展 ffmpeg-php
- The one machine learning concept you need to know
- 原创作品
- mysql 删除日志文件详解
- H3C 无线控制器关闭信号认证
- 让免费版MarkdownPad2使用Pro版本的功能
- Visual Studio对 C++11标准支持情况
- mysql求最大第二,最大第三个数
- Android学习路线总结,绝对干货(转)