- 浏览: 183329 次
- 性别:
- 来自: 济南
文章分类
最新评论
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
之前在Parentheses总结 讲过这道题目,用回溯法解决,想看更多类似的题目可以参考上面那篇文章。代码如下:
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
之前在Parentheses总结 讲过这道题目,用回溯法解决,想看更多类似的题目可以参考上面那篇文章。代码如下:
public class Solution { public List<String> generateParenthesis(int n) { List<String> list = new ArrayList<String>(); int left = n; int right = n; generateP(2 * n, left, right, "", list); return list; } public void generateP(int n, int left, int right, String s, List<String> list) { if(s.length() == n) list.add(s); if(left > 0) generateP(n, left - 1, right, s + '(', list); if(right > left) generateP(n, left, right - 1, s + ')', list); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 264Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 266You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 382Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 373Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 562Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 474Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 662Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 468The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 428Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 573Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 425All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 895Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 928Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 781You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Generate Parentheses.java
第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without ...22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26. Remove Dupli
Generate Parentheses 生成括号 Hamiltonian Cycle 哈密顿循环 Knight Tour骑士之旅 Match Word Pattern 匹配单词模式 Minimax极大 极小 N QueensN 皇后区 N Queens Math En 女王的数学 Power Sum功效和度 Rat In ...
java入门 java_leetcode题解之22_Generate_Parentheses
js js_leetcode题解之22-generate-parentheses.js
c语言入门 C语言_leetcode题解之22-generate-parentheses.c
237 Generate Parentheses 575 238 Combination Sum 577 239 Combination Sum II 579 240 Combination Sum III 581 241 Combinations 583 242 Letter Combinations of a Phone Number 587 243 Restore IP Addresses ...
第22题,"括号生成"(Generate Parentheses),是其中一道经典的动态规划问题,旨在考察程序员对递归、回溯以及组合的理解。这道题目要求我们生成所有有效的括号组合,即所有符合括号开闭规则的字符串。 题目描述:...
* 生成括号(Generate Parentheses):生成所有可能的括号组合。 6. 字符串匹配: * 正则表达式匹配(Regular Expression Matching):实现正则表达式匹配。 * 实现strStr()(Implement strStr()):实现字符串...
18. GenerateParentheses: 生成所有可能的有效的括号组合,这涉及到回溯算法。 19. MergekSortedLists: 合并k个已排序链表,这需要用到优先队列或者归并排序的思想。 20. SwapNodesinPairs: 在链表中交换每两个...
leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** Leetcode 题目列表 ...Parentheses ...Generate Parentheses 028 Implement strStr() 031 Next Permutat
lru缓存leetcode leetcode 1. Two Sum 2. Add Two ...Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3
- Generate Parentheses: 生成所有可能的有效的括号组合。 - Merge k Sorted Lists: 合并k个排序链表。 - Swap Nodes in Pairs / Reverse Nodes in k-Group: 这两个问题涉及到在链表中按特定规则交换节点或反转节点...
Parentheses | ImplementstrStr.java //28. Implement strStr() │ LongestIncreasingSubsequence.java 300.Longest Increasing Subsequence | LongestPalindromicSubstring.java 5. Longest Palindromic Substring...
leetcode 2 LeetCode-练习 我的 ...Generate Parentheses 运行时间:164 毫秒内存使用:22.5 MB 26. Remove Duplicates from Sorted Array 运行时间:100 毫秒内存使用:15.2 MB 27. Remove Element
Generate Parentheses 18 3 sum 扩展版, 外层多套一个循环即可。注意判断重复及细节优化 19 细节:nodelist前插入一个dummy node. 删除倒数第n 等于正数查到len - n 20 用stack 最后stack应该是空的 21 遍历直到二...