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刷題實戰(zhàn)216:組合總和 III

        共 3864字,需瀏覽 8分鐘

         ·

        2021-03-20 12:24

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

        今天和大家聊的問題叫做 組合總和 III,我們先來看題面:
        https://leetcode-cn.com/problems/combination-sum-iii/

        Find all valid combinations of k numbers that sum up to n such that the following conditions are true:


        Only numbers 1 through 9 are used.

        Each number is used at most once.


        Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.

        題意


        找出所有相加之和為 n 的 k 個數(shù)的組合。組合中只允許含有 1 - 9 的正整數(shù),并且每種組合中不存在重復(fù)的數(shù)字。
        說明:
        • 所有數(shù)字都是正整數(shù)。

        • 解集不能包含重復(fù)的組合。 


        示例


        示例 1:

        輸入: k = 3, n = 7
        輸出: [[1,2,4]]

        示例 2:

        輸入: k = 3, n = 9
        輸出: [[1,2,6], [1,3,5], [2,3,4]]


        解題


        碰到這種題直接用DFS一波帶走,結(jié)束遞歸的條件-成功:和為n且組合個數(shù)為k,失敗:和大于n或者組合個數(shù)大于k。

        需要在每次找到組合后刪除最后一個添加元素,才能結(jié)束這輪遞歸;每輪在遞歸結(jié)束必須把當(dāng)前最后一個元素刪除。

        因為所求為組合,且數(shù)字不能重復(fù),所以設(shè)置變量start,從1開始往下走,每次遞歸start的值為當(dāng)前所選值+1

        class Solution {
            private static List<List<Integer>> list;
            private static List<Integer> tem;
            public List<List<Integer>> combinationSum3(int k, int n) {
                list = new ArrayList<List<Integer>>();
                tem = new ArrayList<>();
                dfs(k,n,0,0,1);
                return list;
            }
         
            public void dfs(int k, int n,int sum,int cnt,int start) {
                for (int i = start; i < 10; i++) {
                    if(sum+i > n || tem.size() > k) {
                        return;
                    }
                    tem.add(i);
                    if(sum+i == n && tem.size() == k) {
                        List<Integer> tem0 = new ArrayList<>();
                        tem0.addAll(tem);
                        list.add(tem0);
                        tem.remove(tem.size()-1);
                        return;
                    }
                    dfs(k,n,sum+i,cnt+1,i+1);
                    if(tem.size() > 0) {
                        tem.remove(tem.size()-1);
                    }
                }
            }
        }


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

        上期推文:

        LeetCode1-200題匯總,希望對你有點幫助!

        LeetCode刷題實戰(zhàn)201:數(shù)字范圍按位與

        LeetCode刷題實戰(zhàn)202:快樂數(shù)

        LeetCode刷題實戰(zhàn)203:移除鏈表元素

        LeetCode刷題實戰(zhàn)204:計數(shù)質(zhì)數(shù)

        LeetCode刷題實戰(zhàn)205:同構(gòu)字符串

        LeetCode刷題實戰(zhàn)206:反轉(zhuǎn)鏈表

        LeetCode刷題實戰(zhàn)207:課程表

        LeetCode刷題實戰(zhàn)208:實現(xiàn) Trie (前綴樹)

        LeetCode刷題實戰(zhàn)209:長度最小的子數(shù)組

        LeetCode刷題實戰(zhàn)210:課程表 II

        LeetCode刷題實戰(zhàn)211:添加與搜索單詞

        LeetCode刷題實戰(zhàn)212:單詞搜索 II

        LeetCode刷題實戰(zhàn)213:打家劫舍 II

        LeetCode刷題實戰(zhàn)214:最短回文串

        LeetCode刷題實戰(zhàn)215:數(shù)組中的第K個最大元素


        瀏覽 51
        點贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        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>
            久久99久久精品国产香蕉直播 | 国产人妻精品导航 | 韩国大乳女喂男人吃奶 | 精品女人一区二区三区 | 12男生被小太正裸体动漫网站 | 国产综合激情 | 肉丝袜脚交视频一区二区 | av91探花| 欧美老妇 日韩 | 国产欧美日韩综合精品 |