---
# System prepended metadata

title: 0022. Generate Parentheses
tags: [Leetcode, Dynamic Programming, Parentheses, Medium]

---

# 0022. Generate Parentheses
###### tags: `Leetcode` `Medium` `Dynamic Programming` `Parentheses`
Link: https://leetcode.com/problems/generate-parentheses/
## 思路
## Java Code
```java=
class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> ans = new ArrayList();
        if(n == 0){
            ans.add("");
        }
        else{
            for(int c = 0;c < n;c++){
                for(String left: generateParenthesis(c)){
                    for(String right:generateParenthesis(n-c-1)){
                        ans.add("("+left+")"+right);
                    }
                }
            }
        }
        return ans;
    }
}
```
## Result
Runtime: 9 ms, faster than **8.48%** of Java online submissions for Generate Parentheses.
Memory Usage: 39.3 MB, less than **36.66%** of Java online submissions for Generate Parentheses.