-
22. Generate ParenthesesAlgorithm/java tip 2021. 3. 6. 21:55
leetcode.com/problems/generate-parentheses/
class Solution { int max; List<String> ans = new ArrayList<>(); public List<String> generateParenthesis(int n) { max = n; back_track(0, 0, ""); return ans; } public void back_track(int open, int close, String letter) { //종료 조건 if(letter.length() == max*2) { ans.add(letter); return; } if(open < max) { back_track(open + 1, close, letter + "("); } if(close < open) { back_track(open, close + 1, letter + ")"); } } }
'Algorithm > java tip' 카테고리의 다른 글
78. Subsets (0) 2021.03.07 404. Sum of Left Leaves (0) 2021.03.06 559. Maximum Depth of N-ary Tree (0) 2021.03.06 [programmers] 가장 먼 노드 (0) 2021.03.06 [programmers] 큰 수 만들기 (0) 2021.03.06