# 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.
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up