There is my cpp, the result's number and content is right,but the order is not same as the answer.


  • 0
    L
    vector<string> generateParenthesis(int n) {
        vector<string> svec;
    	if (n == 1)
    	{
    		svec.push_back("()");
    		return svec;
    	}
    	svec = generateParenthesis(n - 1);
    	int size = svec.size();
    	
    	for (int i = 0; i<size; i++)
    	{
    		string temp = svec[i] + "()";
    		svec.push_back(temp);
    		temp = "()" + svec[i];
    		svec.push_back(temp);
    		temp = "(" + svec[i] + ")";
    		svec.push_back(temp);
    	}
    	while(size--)
    	    svec.erase(svec.begin());
    	auto iter = unique(svec.begin(), svec.end());
    	svec.erase(iter, svec.end());
    
    	return svec;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.