成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] 22. Generate Parentheses

curlyCheng / 2581人閱讀

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 List generateParenthesis(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

相關(guān)文章

  • LeetCode[22] Generate Parentheses

    摘要:復(fù)雜度思路注意的地方,要限制左括號和右括號。每出現(xiàn)一次左括號,就相對于限定了,最多只能出現(xiàn)那么多右括號。所以,為了完成這種限定,用來控制。不然會有的情況出現(xiàn)。 LeetCode[22] Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

    Jonathan Shieber 評論0 收藏0
  • leetcode22. Generate Parentheses

    摘要:當右括號和左括號的剩余量均為時,及為一個最終結(jié)果。而則會在直接原來的對象上進行修改,其指針仍然指向原來的對象。因此在遞歸的過程中使用一定要注意,對對象的修改不要相互干擾。 題目要求 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses....

    騫諱護 評論0 收藏0
  • leetcode 22 Generate Parentheses

    摘要:要求返回一個中包含組括號所有可能的符合規(guī)則的組合。例如輸入結(jié)果集應(yīng)當是想法輸入的就代表著我們的字符串的組成是個和個。我們需要跟蹤和的使用情況,來判斷下一步的操作是否合法。 題目詳情 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses....

    figofuture 評論0 收藏0
  • leetcode 部分解答索引(持續(xù)更新~)

    摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當時也沒有按順序?qū)懍F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當時也沒有按順序?qū)憽F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個索引嘻嘻。 順序整理 1~50 1...

    leo108 評論0 收藏0
  • 構(gòu)造n個成對括號

    摘要:構(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...

    姘擱『 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<