LeetCode 22 Generate Parentheses (C,C++,Java,Python)
来源:互联网 发布:java全排列 编辑:程序博客网 时间:2024/06/07 08:38
Problem:
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
Solution:
递归遍历即可,注意两个条件,要插入‘(’的时候一定要剩余的数目大于0,要插入‘)’的时候一定要剩余的数目大于0 并且剩余的)的数目大于剩余的( 的数目
题目大意:
给一个数目n要求组合成一个由n个括号组成的字符串,要求输出所有的可能排列情况。
Java源代码(252ms):
public class Solution { public List<String> generateParenthesis(int n) { List<String> res = new ArrayList<String>(); char[] tmp=new char[n+n]; generate(res,n,n,tmp,0); return res; } private void generate(List<String> res,int l,int r,char[] tmp,int index){ if(l==0 && r==0){ res.add(new String(tmp)); return; } if(l>0){ tmp[index]='('; generate(res,l-1,r,tmp,index+1); } if(r>0 && r>l){ tmp[index]=')'; generate(res,l,r-1,tmp,index+1); } }}
C语言源代码(2ms):
void generate(char** res,int* size,int l,int r,char* tmp,int index){ if(l==0 && r==0){ tmp[index]=0; res[*size]=(char*)malloc(sizeof(char)*index); strcpy(res[*size],tmp); (*size)++; return; } if(l>0){ tmp[index]='('; generate(res,size,l-1,r,tmp,index+1); } if(r>0 && l<r){ tmp[index]=')'; generate(res,size,l,r-1,tmp,index+1); }}char** generateParenthesis(int n, int* returnSize) { char** res; char* tmp=(char*)malloc(sizeof(char)*(n*2+1)); int l=n,r=n; res=(char**)malloc(sizeof(char*)*1000000); *returnSize=0; generate(res,returnSize,l,r,tmp,0); return res;}
C++源代码(4ms):
class Solution {public: vector<string> generateParenthesis(int n) { vector<string> res; char* tmp=(char*)malloc(sizeof(char)*(n+n+1)); generate(res,n,n,tmp,0); return res; }private: void generate(vector<string>& res,int l,int r,char* tmp,int index){ if(l==0 && r==0){ tmp[index]=0; res.push_back(string(tmp)); return; } if(l>0){ tmp[index]='('; generate(res,l-1,r,tmp,index+1); } if(r>0 && r>l){ tmp[index]=')'; generate(res,l,r-1,tmp,index+1); } }};
Python源代码(57ms):
class Solution: # @param {integer} n # @return {string[]} def generateParenthesis(self, n): res=[] tmp=['' for i in range(n+n)] self.generate(res,n,n,tmp,0) return res def generate(self,res,l,r,tmp,index): if l==0 and r==0: res.append(''.join(tmp)) return if l>0: tmp[index]='(' self.generate(res,l-1,r,tmp,index+1) if r>0 and r>l: tmp[index]=')' self.generate(res,l,r-1,tmp,index+1)
0 0
- LeetCode 22 Generate Parentheses (C,C++,Java,Python)
- [LeetCode] 022. Generate Parentheses (Medium) (C++/Java/Python)
- 【C++】【LeetCode】22. Generate Parentheses
- [python]leetcode(22). Generate Parentheses
- LeetCode 22 -Generate Parentheses ( JAVA )
- LeetCode之Generate Parentheses(C++)
- 【LeetCode算法练习(C++)】Generate Parentheses
- 【Leetcode】【python】Generate Parentheses
- LeetCode 22 — Generate Parentheses(C++ Java Python)
- [Leetcode] Generate Parentheses (Java)
- [LeetCode][Java] Generate Parentheses
- [leetcode]Generate Parentheses(java)
- LeetCode : Generate Parentheses [java]
- leetcode Generate Parentheses(Java)
- LeetCode 22: Generate Parentheses
- [leetcode 22] Generate Parentheses
- LeetCode | #22 Generate Parentheses
- [Leetcode] 22- Generate Parentheses
- 【割点】 HDU 4587 TWO NODES
- 一步一步跟我学习lucene(5)---lucene的索引构建原理
- 【机房重构】——VS制作报表+日期格式转化
- 操作系统 之 进程
- POJ1155---TELE(树形dp,背包)
- LeetCode 22 Generate Parentheses (C,C++,Java,Python)
- Struts2学习笔记(十五)异步传输机制
- 音乐消除国界、下载废除权限:歌曲地址解析器 yosong
- linux平台学x86汇编(十七):在汇编中使用linux系统调用
- Android Studio系列(一)安装与配置错误解决方案(windows)
- Android中活动的四种启动模式
- tomcat server.xml配置
- 第八周上级项目1 复数类中的运算符重载
- 面试中科院通用芯片与基础软件研究中心面试总结