在LeetCode 中有关括号的题共有七道,除了这一道的另外六道是Score of Parentheses,Valid Parenthesis String,Remove Invalid Parentheses,Different Ways to Add Parentheses,Valid Parentheses和Longest Valid Parentheses。这道题给定一个数字n,让生成共有n个括号的所有正确的形式,对于这种列出所有结果的题首先还是考虑用...
【leetcode】22. Generate Parentheses 题目描述: Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses. 解题分析: 这类题一般都要用递归的方法来解决。需要设两个集合类分别存储待匹配的(,)的个数。 这里需要明白一点:当待匹配的(的个数永远不小于待匹配的)的...
class Solution { public: vector<string> generateParenthesis(int n) { vector<string> result; string str = ""; addPar(result,str,n,0); return result; } void addPar(vector<string> & result, string str, int left, int right){ if(left == 0 && right == 0){ result.push_back(str); ...
public List<String> generateParenthesis(int n) { this.c = new char[n * 2]; this.n = n * 2; dspGener(0); return strList; } List<String> strList = new ArrayList<String>(); char c[];//暂存中间结果 char kuohao[] = new char[] { '(', ')' }; int n = 0; void dspGener...
自己没想出来,全部参考 LeetCode 给出的Solution。 解法一 暴力破解 列举所有的情况,每一位有左括号和右括号两种情况,总共 2n 位,所以总共 种情况。 publicList<String>generateParenthesis(intn){List<String>combinations=newArrayList();generateAll(newchar[2*n],0,combinations);returncombinations;}publicvoidgene...
1. Description Generate Parentheses 2. Solution class Solution{public:vector<string>generateParenthesis(intn){vector<string>result;push(0,0,result,"",n,'(');returnresult;}private:voidpush(intleft,intright,vector<string>&result,string s,constintn,charch){s+=ch;if(ch=='('){left++;}else{...
. - 备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。
简介:题目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:"((()))", "(()())", "(())()", "()(())", "()() 题目 Givennpairs of parentheses, write a function to generate all ...
Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, givenn= 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] publicclassSolution {privateList<String> list =newArrayList<String>();...
题目:http://oj.leetcode.com/problems/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 ...