【Leetcode-Medium-22】Generate Parentheses
来源:互联网 发布:手机淘宝没有评价订单 编辑:程序博客网 时间:2024/05/22 03:14
【Leetcode-Medium-22】Generate Parentheses
题目
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:
[
“((()))”,
“(()())”,
“(())()”,
“()(())”,
“()()()”
]
思路
backtracking solution,回溯The idea here is to only add '(' and ')' that we know will guarantee us a solution (instead of adding 1 too many close).Once we add a '(' we will then discard it and try a ')' which can only close a valid '('. Each of these steps are recursively called.
程序
import java.util.*;class Solution { /** backtracking solution,回溯 The idea here is to only add '(' and ')' that we know will guarantee us a solution (instead of adding 1 too many close). Once we add a '(' we will then discard it and try a ')' which can only close a valid '('. Each of these steps are recursively called. */ public List<String> generateParenthesis(int n) { List<String> list = new ArrayList<>(); backtrack(list, "", 0, 0, n); return list; } private 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); } //------------------------------------------------ // Time limit exceeded /* 思路: 排列组合出所有的字符串 验证每一个字符串是否合格 */ public List<String> generateParenthesis0(int n) { char[] chs = new char[n*2]; for (int i = 0; i < n; i ++){ chs[i] = '('; chs[n+i] = ')'; } Set<String> set = new HashSet<>(); permutate(chs, 0, set); return new ArrayList<String>(set); } // 字符串全排列 private void permutate(char[] chs, int start, Set<String> set){ if (start == chs.length){ boolean bool = validate(chs); if (bool) set.add(new String(chs)); } for (int i = start; i < chs.length; i ++){ char temp = chs[start]; chs[start] = chs[i]; chs[i] = temp; permutate(chs, start+1, set); temp = chs[start]; chs[start] = chs[i]; chs[i] = temp; } } private boolean validate(char[] chs){ LinkedList<Character> stack = new LinkedList<>(); for (char ch : chs){ if (ch == ')') { if (stack.isEmpty() || stack.pop() != '(') return false; } else { stack.push(ch); } } return stack.isEmpty(); } }
声明:题目来自Leetcode.
阅读全文
1 0
- [Leetcode 22, Medium] Generate Parentheses
- LeetCode #22 - Generate Parentheses - Medium
- 【Leetcode-Medium-22】Generate Parentheses
- LeetCode-22-Generate Parentheses(DFS/递归)-Medium
- Leetcode OJ 22 Generate Parentheses [Medium]
- Leetcode OJ 22 Generate Parentheses [Medium]
- LeetCode 22. Generate Parentheses (Medium)
- Leetcode 22. Generate Parentheses (Medium) (cpp)
- LeetCode 22: Generate Parentheses
- [leetcode 22] Generate Parentheses
- LeetCode | #22 Generate Parentheses
- [Leetcode] 22- Generate Parentheses
- LeetCode 22 Generate Parentheses
- [LeetCode 22]Generate Parentheses
- leetcode 22 Generate Parentheses
- Leetcode 22 Generate Parentheses
- LeetCode 22 - Generate Parentheses
- Generate Parentheses - LeetCode 22
- 对比电容理解电感
- uva315
- ACM复习(3)1139 约瑟夫环问题
- 31. Next Permutation
- selenium之终极封装
- 【Leetcode-Medium-22】Generate Parentheses
- CCS5.5新建工程
- 【Flask】我的第一个Flask程序
- 【LeetCode】Minimum Depth Of Binary Tree二叉树的最小深度
- Linked server和SSIS连接Oracle时的常见问题(三)
- Node.js 路由
- Dapper插入记录性能测试
- sql-server(模式创建)
- 01_自动化Python_学习概述