日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【LeetCode从零单排】No22.Generate Parentheses

發布時間:2025/4/5 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LeetCode从零单排】No22.Generate Parentheses 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

Given?n?pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given?n?= 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

代碼

For 2, it should place one "()" and add another one insert it but none tail it,

'(' f(1) ')' f(0)

or add none insert it but tail it by another one,

'(' f(0) ')' f(1)

Thus for n, we can insert f(i) and tail f(j) and i+j=n-1,

'(' f(i) ')' f(j)

public List<String> generateParenthesis(int n) {List<String> result = new ArrayList<String>();if (n == 0) {result.add("");} else {for (int i = n - 1; i >= 0; i--) {List<String> insertSub = generateParenthesis(i);List<String> tailSub = generateParenthesis(n - 1 - i);for (String insert : insertSub) {for (String tail : tailSub) {result.add("(" + insert + ")" + tail);}}}}return result; }

/********************************

* 本文來自博客 ?“李博Garvin“

* 轉載請標明出處:http://blog.csdn.net/buptgshengod

******************************************/




總結

以上是生活随笔為你收集整理的【LeetCode从零单排】No22.Generate Parentheses的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。