-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
30 lines (27 loc) · 728 Bytes
/
Copy pathsolution.java
File metadata and controls
30 lines (27 loc) · 728 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 22. Generate Parentheses
// https://leetcode.com/problems/generate-parentheses/
// Medium | Java | Accepted 2025-10-27
// Runtime 2 ms | Memory 43.8 MB
class Solution {
public List<String> generateParenthesis(int n) {
List<String> ans = new ArrayList<>();
recursion(ans, 0, 0, "", n);
return ans;
}
public void recursion(List<String> ans, int left, int right, String temp, int n)
{
if(temp.length()==n*2)
{
ans.add(temp);
return;
}
if(left<n)
{
recursion(ans, left+1, right, temp+"(", n);
}
if(right<left)
{
recursion(ans, left, right+1, temp+")", n);
}
}
}