Problem
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:
[ "((()))", "(()())", "(())()", "()(())", "()()()" ]Solution
class Solution { public ListgenerateParenthesis(int n) { List res = new ArrayList<>(); helper(0, 0, n, "", res); return res; } private void helper(int left, int right, int total, String str, List res) { if (str.length() == total*2) { res.add(str); return; } if (left < total) { helper(left+1, right, total, str+"(", res); } if (right < left) { helper(left, right+1, total, str+")", res); } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/77296.html
摘要:復(fù)雜度思路注意的地方,要限制左括號和右括號。每出現(xiàn)一次左括號,就相對于限定了,最多只能出現(xiàn)那么多右括號。所以,為了完成這種限定,用來控制。不然會有的情況出現(xiàn)。 LeetCode[22] Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
摘要:當右括號和左括號的剩余量均為時,及為一個最終結(jié)果。而則會在直接原來的對象上進行修改,其指針仍然指向原來的對象。因此在遞歸的過程中使用一定要注意,對對象的修改不要相互干擾。 題目要求 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses....
摘要:要求返回一個中包含組括號所有可能的符合規(guī)則的組合。例如輸入結(jié)果集應(yīng)當是想法輸入的就代表著我們的字符串的組成是個和個。我們需要跟蹤和的使用情況,來判斷下一步的操作是否合法。 題目詳情 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses....
摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當時也沒有按順序?qū)懍F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當時也沒有按順序?qū)憽F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個索引嘻嘻。 順序整理 1~50 1...
摘要:構(gòu)造個成對括號給出一個整數(shù),實現(xiàn)一個函數(shù)生成對小括號,對小括號的左右括弧順序不限,但應(yīng)該閉合。思路的情況為時的括號串中在縫隙位置再插入一個括號,如中位置。遞歸解決,時為在和中再插入一個括號。 構(gòu)造n個成對括號 Generate Parentheses 給出一個整數(shù)n,實現(xiàn)一個函數(shù)生成n對小括號,n對小括號的左右括弧順序不限,但應(yīng)該閉合。 Given n pairs of parent...
閱讀 798·2021-10-14 09:42
閱讀 2026·2021-09-22 15:04
閱讀 1669·2019-08-30 12:44
閱讀 2213·2019-08-29 13:29
閱讀 2790·2019-08-29 12:51
閱讀 605·2019-08-26 18:18
閱讀 777·2019-08-26 13:43
閱讀 2873·2019-08-26 13:38