亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

leetcode 22 Generate Parentheses

figofuture / 2095人閱讀

摘要:要求返回一個(gè)中包含組括號(hào)所有可能的符合規(guī)則的組合。例如輸入結(jié)果集應(yīng)當(dāng)是想法輸入的就代表著我們的字符串的組成是個(gè)和個(gè)。我們需要跟蹤和的使用情況,來(lái)判斷下一步的操作是否合法。

題目詳情
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

輸入一個(gè)正整數(shù)n。要求返回一個(gè)List,list中包含n組括號(hào)所有可能的符合規(guī)則的組合。如“(())”就屬于符合規(guī)則的組合,“)(()”就屬于不符合規(guī)則的組合。

例如, 輸入 n = 3, 結(jié)果集應(yīng)當(dāng)是:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

想法

輸入的n就代表著我們的字符串的組成是n個(gè)"("和n個(gè)")"。

聲明一個(gè)cur字符串來(lái)暫存我們沒次操作獲得的字符串,而我們每次想要將括號(hào)加入cur字符串的時(shí)候,我們要保證剩余的括號(hào)和現(xiàn)有字符串,可以滿足規(guī)則。也就是說(shuō),如果我們想加入一個(gè)")",我們要保證cur字符串中的")"的數(shù)量小于"("的數(shù)量,否則字符串就不符合規(guī)則了。

我們需要跟蹤"("和")"的使用情況,來(lái)判斷下一步的操作是否合法。

解法
     public List generateParenthesis(int n) {
            List ans = new ArrayList();
            backtrack(ans, "", 0, 0, n);
            return ans;
        }

        public void backtrack(List ans, String cur, int open, int close, int max){
            if (cur.length() == max * 2) {
                ans.add(cur);
                System.out.println(cur);
                return;
            }

            if (open < max)
                backtrack(ans, cur+"(", open+1, close, max);
            if (close < open)
                backtrack(ans, cur+")", open, close+1, max);
        }
    

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/68778.html

相關(guān)文章

  • LeetCode[22] Generate Parentheses

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

    Jonathan Shieber 評(píng)論0 收藏0
  • [LeetCode] 22. Generate Parentheses

    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: [ ((())), (()()), (())(), ()(()), ...

    curlyCheng 評(píng)論0 收藏0
  • leetcode22. Generate Parentheses

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

    騫諱護(hù) 評(píng)論0 收藏0
  • [Leetcod] Generate Parentheses 產(chǎn)生括號(hào)

    摘要:而對(duì)于右括號(hào),必須前面放了一個(gè)左括號(hào),我們才能放一個(gè)右括號(hào)。所以我們根據(jù)剩余可放置左括號(hào),和剩余可放置右括號(hào),代入遞歸,就可以求解。 Generate Parentheses 最新更新請(qǐng)見:https://yanjia.me/zh/2019/01/... Given n pairs of parentheses, write a function to generate all co...

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

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

    leo108 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<