티스토리 뷰

https://leetcode.com/problems/generate-parentheses/

 

Generate Parentheses - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

 

[leetcode][C++] generate parentheses

 

class Solution {
public:
	void dfs(int l, int r, int n, string s, vector<string>& ret) {
		if (l == n && r == n) {
			ret.push_back(s);
			return;
		}

		if (l < n)
			dfs(l + 1, r, n, s + '(', ret);
		if (r<n && l>r)
			dfs(l, r + 1, n, s + ')', ret);
	}

	vector<string> generateParenthesis(int n) {
		vector<string> ret;
		string s = "(";

		dfs(1, 0, n, s, ret);

		return ret;
	}
};
댓글