티스토리 뷰
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;
}
};
'Problem Solving' 카테고리의 다른 글
[leetcode][C++] sort colors (0) | 2021.10.15 |
---|---|
[leetcode][C++][Java] jump game (0) | 2021.10.15 |
[leetcode][C++] search in rotated sorted array (0) | 2021.10.06 |
[leetcode][C++][Java] letter combinations of a phone number (0) | 2021.10.05 |
[프로그래머스][C++] 모음사전 (0) | 2021.10.04 |
댓글