1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實(shí)戰(zhàn)39:組合總和

        共 2331字,需瀏覽 5分鐘

         ·

        2020-09-16 21:18

        算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !


        今天和大家聊的問(wèn)題叫做?組合總和,我們先來(lái)看題面:

        https://leetcode-cn.com/problems/combination-sum

        Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.


        The same repeated number may be chosen from candidates unlimited number of times.


        Note:

        All numbers (including target) will be positive integers.

        The solution set must not contain duplicate combinations.

        題意


        給定一個(gè)無(wú)重復(fù)元素的數(shù)組 candidates 和一個(gè)目標(biāo)數(shù) target ,找出 candidates 中所有可以使數(shù)字和為 target 的組合。
        candidates 中的數(shù)字可以無(wú)限制重復(fù)被選取。
        說(shuō)明:
        所有數(shù)字(包括 target)都是正整數(shù)。
        解集不能包含重復(fù)的組合。?

        樣例

        示例?1

        輸入:candidates = [2,3,6,7], target = 7,
        所求解集為:
        [
        ??[7
        ],
        ??[2,2,3]
        ]

        示例?2

        輸入:candidates = [2,3,5], target = 8,
        所求解集為:
        [
        ? [2,2,2,2
        ],
        ? [2,3,3],
        ? [3,5]
        ]

        題解

        回溯法

        回溯算法關(guān)鍵在于:不合適就退回上一步,然后通過(guò)約束條件, 減少時(shí)間復(fù)雜度.

        假設(shè)candidates = [2, 3, 6, 7],target = 7
        以 target = 7 為根結(jié)點(diǎn),每一個(gè)分支做減法。減到 0 或者負(fù)數(shù)的時(shí)候,剪枝。其中,減到 0 的時(shí)候結(jié)算,這里 “結(jié)算” 的意思是添加到結(jié)果集。



        代碼如下:

        class?Solution?{
        ????public?List> combinationSum(int[] candidates, int?target) {
        ????????List> res = new?ArrayList<>();
        ????????Arrays.sort(candidates);
        ????????backtrack(candidates, target, res, 0, new?ArrayList());
        ????????return?res;
        ????}
        ????private?void?backtrack(int[] candidates, int?target, List> res,
        ????????int?i, ArrayList tmp_list
        )
        {
        ????????if?(target < 0) return;
        ????????if?(target == 0) {
        ????????????res.add(new?ArrayList<>(tmp_list)); return;
        ????????}
        ????????for?(int?start = i; start < candidates.length; start++) {
        ????????????if?(target < candidates[start]) break;
        ????????????tmp_list.add(candidates[start]);
        ????????????backtrack(candidates, target - candidates[start], res, start, tmp_list);
        ????????????tmp_list.remove(tmp_list.size() - 1);
        ????????}
        ????}
        }


        好了,今天的文章就到這里,如果覺(jué)得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力。


        上期推文:


        LeetCode1-20題匯總,速度收藏!
        LeetCode刷題實(shí)戰(zhàn)21:合并兩個(gè)有序鏈表
        LeetCode刷題實(shí)戰(zhàn)23:合并K個(gè)升序鏈表
        LeetCode刷題實(shí)戰(zhàn)24:兩兩交換鏈表中的節(jié)點(diǎn)
        LeetCode刷題實(shí)戰(zhàn)25:K 個(gè)一組翻轉(zhuǎn)鏈表
        LeetCode刷題實(shí)戰(zhàn)26:刪除排序數(shù)組中的重復(fù)項(xiàng)
        LeetCode刷題實(shí)戰(zhàn)27:移除元素
        LeetCode刷題實(shí)戰(zhàn)28:實(shí)現(xiàn) strStr()
        LeetCode刷題實(shí)戰(zhàn)29:兩數(shù)相除
        LeetCode刷題實(shí)戰(zhàn)30:串聯(lián)所有單詞的子串
        LeetCode刷題實(shí)戰(zhàn)31:下一個(gè)排列
        LeetCode刷題實(shí)戰(zhàn)32:最長(zhǎng)有效括號(hào)
        LeetCode刷題實(shí)戰(zhàn)33:搜索旋轉(zhuǎn)排序數(shù)組
        LeetCode刷題實(shí)戰(zhàn)34:在排序數(shù)組中查找元素
        LeetCode刷題實(shí)戰(zhàn)35:搜索插入位置
        LeetCode刷題實(shí)戰(zhàn)36:有效的數(shù)獨(dú)
        LeetCode刷題實(shí)戰(zhàn)37:解數(shù)獨(dú)
        LeetCode刷題實(shí)戰(zhàn)38:外觀數(shù)列


        瀏覽 46
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            中国操逼大片 | 精品国产一区二区三区不卡蜜臂 | ccavcc.com | 成人AAAAA片免费观看 | 中国av少妇 | 免费视频久久久久久久 | 我慢慢进入岳的两片间 | 做爱网站网站入口在线观看免费 | 日韩一区二区三区电影在线观看 | av小说网 |