BM60. 括号生成
描述
给出n对括号,请编写一个函数来生成所有的由n对括号组成的合法组合。示例1
输入:
1
输出:
["()"]
示例2
输入:
2
输出:
["(())","()()"]
C++ 解法, 执行用时: 0ms, 内存消耗: 8552KB, 提交时间: 2015-03-26
class Solution { public: void dfs(int x,int y,int tot,int n,char *s,vector<string>&res){ if(x==n&&y==n){ s[tot]='\0'; //cout<<s<<endl; string ss=s; res.push_back(ss); return ; } if(x<n){ s[tot]='('; dfs(x+1,y,tot+1,n,s,res); } if(x>y){ s[tot]=')'; dfs(x,y+1,tot+1,n,s,res); } } vector<string> generateParenthesis(int n) { char s[100]; vector<string>res; dfs(0,0,0,n,s,res); return res; } };
C++ 解法, 执行用时: 0ms, 内存消耗: 8552KB, 提交时间: 2015-03-26
vector<string> res; int cnt; void build(vector<char> s, int l,int v){ if (s.size() == cnt * 2){ string t = ""; for (int i = 0; i < s.size(); i++) t += s[i]; res.push_back(t); return; } if (l < cnt){ vector<char> a = s; a.push_back('('); build(a, l + 1, v + 1); } if (v > 0){ vector<char> a = s; a.push_back(')'); build(a, l, v - 1); } } class Solution { public: vector<string> generateParenthesis(int n) { res.clear(); cnt = n; vector<char> e; build(e, 0, 0); return res; } };
C 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-06-02
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return string字符串一维数组 * @return int* returnSize 返回数组行数 * * C语言声明定义全局变量请加上static,防止重复定义 */ static int hang = 0; void dfs(char** resu, int n, int left, int right, char str[]) { if(left == n && right == n) { char* tmp = (char*)malloc(sizeof(char)*n*2); memcpy(tmp, str, n*2); resu[hang++] = tmp; return ; } if(left < n) { str[left+right] = '('; dfs(resu, n, left+1, right, str); } if(right < n && right < left) { str[left+right] = ')'; dfs(resu, n, left, right+1, str); } return ; } char** generateParenthesis(int n, int* returnSize) { // write code here if(n == 0) return NULL; char** resu = (char**)malloc(sizeof(char*)*100000); int left = 0; int right = 0; char str[21] = {'\0'}; dfs(resu, n, left, right, str); * returnSize = hang; return resu; }
Rust 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-06-26
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return string字符串一维数组 */ pub fn generateParenthesis(&self, n: i32) -> Vec<String> { // write code here let mut store = vec![]; let mut s = String::new(); let mut n = n as usize; produce(&mut store, n, n, &mut s); return store; } } fn produce(store: &mut Vec<String>, l: usize, r: usize, s: &mut String) { if l == 0 && r == 0 { store.push(s.clone()); return; } if l == 0 && r > 0 { s.push(')'); produce(store, l, r - 1, s); s.pop(); return; } if r == 0 && l > 0 { s.push('('); produce(store, l - 1, r, s); s.pop(); return; } if l < r { s.push('('); produce(store, l - 1, r, s); s.pop(); s.push(')'); produce(store, l, r - 1, s); s.pop(); return; } if l >= r { s.push('('); produce(store, l - 1, r, s); s.pop(); } }
C++ 解法, 执行用时: 3ms, 内存消耗: 412KB, 提交时间: 2022-07-27
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return string字符串vector */ void recursion(int left, int right, vector<string>& res, string temp, int n) { if(left == n && right == n) { res.push_back(temp); return; } if(left < n) { recursion(left + 1, right, res, temp + "(", n); } if(right < n && left > right) recursion(left, right + 1, res, temp + ")", n); } vector<string> generateParenthesis(int n) { vector<string> res; string temp = ""; recursion(0, 0, res, temp, n); return res; } };