LeetCode进阶之路(Generate Parentheses)
来源:互联网 发布:淘宝阿苏纳刷怪升级 编辑:程序博客网 时间:2024/05/01 09:28
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:
[ "((()))", "(()())", "(())()", "()(())", "()()()"]
题目:给一个整数n,列出n组括号符合parentheses的排列。
思路:假如n=3,列出三组括号所有的可能性,根据之前isValid找出符合要求的排列即可,但是这种方法明显会超时,中间步骤太多太繁琐。
public class Solution { List<String> list = new ArrayList<String>(); public List<String> generateParenthesis(int n){List<String> l = new ArrayList<String>();String s = "()";StringBuffer sb = new StringBuffer();for(int i = 0; i < n;i++) {sb.append(s);}char[] ctr = new char[n*2];for(int i = 0; i < n*2 ; i++) {ctr[i] = sb.charAt(i);}arrange(ctr, 0);for(String st : list) {if(isValid(st)) {l.add(st);}}return l;}public void arrange(char[] str, int i) {if(i >= str.length) {return;}if(i == str.length - 1){if(!list.contains(String.valueOf(str))) {list.add(String.valueOf(str));}} else {for(int j = i; j < str.length; j++) {char tmp = str[j];str[j] = str[i];str[i] = tmp;arrange(str, i+1);tmp = str[j];str[j] = str[i];str[i] = tmp;}}}public boolean isValid(String s) {Map<Character, Character> map = new HashMap<Character, Character>();map.put('(', ')');map.put('{', '}');map.put('[', ']');Stack<Character> stack = new Stack<Character>();for(int i = 0; i < s.length();i++) {char curr = s.charAt(i);if(map.containsKey(curr)){stack.push(curr);continue;}if(map.containsValue(curr) && !stack.isEmpty() && map.get(stack.peek())== curr) {stack.pop();} else {return false;}}return stack.isEmpty();}
思路二:这个是参考网友的做法,是根据DSF(深度有限算法)来处理的,加上递归相当简单。
public class Solution { public List<String> generateParenthesis(int n) { if(n<=0)return null; ArrayList<String>list = new ArrayList<String>(); String str = new String(); dfs(list,str,n,n); return list; } private voiddfs(ArrayList<String> list, String str, int left, int right){ if(left>right)return;//problem with ")(" if(left==0&&right==0){ list.add(str); } if(left>0)dfs(list,str+"(",left-1,right); if(right>0)dfs(list, str+")",left,right-1); }}
有时间需要研究下深度有限算法和广度优先算法,毕业找工作时研究过,现在却都还回去了。
0 0
- LeetCode进阶之路(Generate Parentheses)
- leetcode之 Generate Parentheses
- leetcode之Generate Parentheses
- leetcode之 Generate Parentheses
- 【Leetcode】之Generate Parentheses
- leetcode之路022 Generate Parentheses
- LeetCode之Generate Parentheses(C++)
- LeetCode之22----Generate Parentheses
- LeetCode(22)Generate Parentheses
- 《leetCode》:Generate Parentheses(hard)
- (Leetcode)22. Generate Parentheses
- LeetCode (22)Generate Parentheses
- Leetcode-Generate Parentheses(递归)
- 《leetCode》:Generate Parentheses(hard)
- LeetCode OJ 之 Generate Parentheses (产生括号)
- LeetCode进阶之路(Valid Parentheses)
- leetcode之 Generate Parentheses(Catalan数)
- LeetCode | Generate Parentheses(生成括号)
- struts配置文件中结果类型小结
- android开发性能优化经验总结一
- centos 创建软连接后,打开软连接文件,报错:Too many levels of symbolic link
- POJ2606&1118 同直线最多点数
- 数据结构之希尔排序
- LeetCode进阶之路(Generate Parentheses)
- hbase集群搭建后感悟
- 计算机网络——数据链路层(4)
- JavaScript 基础知识总结归纳(一)
- JHTP小结_第十二章_GUI组件(上篇)-Part 1
- Hust oj 1101 Bombs of HRBUST(Prim)
- 直流电和交流电
- xUtils源码解析
- 我的第1台Iphone与MacBook