#include <string> #include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return string字符串vector */ vector<string> generateParenthesis(int n) { // write code here vector<string> res; if(n==0) return res; string temp; recursion(0, 0, temp, res, n); return res; } void recursion(int left,int right,string& temp,vector<string>& res,int n){ //使用一次左括号 if(left==n&&right==n){ res.push_back(temp); } if(left < n){ string str=temp + "("; recursion(left + 1, right, str, res, n); } //使用右括号个数必须少于左括号 if(right < n && left > right){ string str=temp + ")"; recursion(left, right + 1, str, res, n); } } };