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)254:因子的組合

        共 3484字,需瀏覽 7分鐘

         ·

        2021-05-05 06:59

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

        今天和大家聊的問題叫做 因子的組合,我們先來看題面:
        https://leetcode-cn.com/problems/factor-combinations/

        Numbers can be regarded as product of its factors. For example,

        8 = 2 x 2 x 2;

           = 2 x 4.

        Write a function that takes an integer n and return all possible combinations of its factors.

        請實(shí)現(xiàn)一個(gè)函數(shù),該函數(shù)接收一個(gè)整數(shù) n 并返回該整數(shù)所有的因子組合。

        示例


        注意:
        你可以假定 n 為永遠(yuǎn)為正數(shù)。
        因子必須大于 1 并且小于 n

        示例 1
        輸入: 1
        輸出: []

        示例 2
        輸入: 37
        輸出: []

        示例 3
        輸入: 12
        輸出:
        [
          [2, 6]
        ,
          [2, 2, 3],
          [3, 4]
        ]

        示例 4:
        輸入: 32
        輸出:
        [
          [2, 16]
        ,
          [2, 2, 8],
          [2, 2, 2, 4],
          [2, 2, 2, 2, 2],
          [2, 4, 4],
          [4, 8]
        ]



        解題


        可以采用遞歸。從2開始遍歷到sqrt(n),能被n整除就進(jìn)下一個(gè)遞歸,當(dāng)start超過sqrt(n)時(shí),start變成n,進(jìn)下一個(gè)遞歸。

        public class Solution {
            public List<List<Integer>> getFactors(int n) {
                List<List<Integer>> result = new ArrayList<List<Integer>>();
                helper(result, new ArrayList<Integer>(), n, 2);
                return result;
            }
         
            public void helper(List<List<Integer>> result, List<Integer> item, int n, int start){
                if (n <= 1) {
                    if (item.size() > 1) {
                        result.add(new ArrayList<Integer>(item));
                    }
                    return;
                }
                
                for (int i = start; i * i <= n; ++i) {
                    if (n % i == 0) {
                        item.add(i);
                        helper(result, item, n/i, i);
                        item.remove(item.size()-1);
                    }
                }
                
                int i = n;
                item.add(i);
                helper(result, item, 1, i);
                item.remove(item.size()-1);
            }
        }


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

        上期推文:

        LeetCode1-240題匯總,希望對你有點(diǎn)幫助!

        LeetCode刷題實(shí)戰(zhàn)241:為運(yùn)算表達(dá)式設(shè)計(jì)優(yōu)先級

        LeetCode刷題實(shí)戰(zhàn)242:有效的字母異位詞

        LeetCode刷題實(shí)戰(zhàn)243:最短單詞距離

        LeetCode刷題實(shí)戰(zhàn)244:最短單詞距離 II

        LeetCode刷題實(shí)戰(zhàn)245:最短單詞距離 III

        LeetCode刷題實(shí)戰(zhàn)246:中心對稱數(shù)

        LeetCode刷題實(shí)戰(zhàn)247:中心對稱數(shù)II

        LeetCode刷題實(shí)戰(zhàn)248:中心對稱數(shù)III

        LeetCode刷題實(shí)戰(zhàn)249:移位字符串分組

        LeetCode刷題實(shí)戰(zhàn)250:統(tǒng)計(jì)同值子樹

        LeetCode刷題實(shí)戰(zhàn)251:展開二維向量

        LeetCode刷題實(shí)戰(zhàn)252:會(huì)議室

        LeetCode刷題實(shí)戰(zhàn)253:會(huì)議室II


        瀏覽 91
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

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

        手機(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>
            色哟哟啪啪成人网站免费 | 99久久久无码国产精品不片 | 乳尖蹂躏挣扎哀求 | 色九九 | 国产欧美日韩一级大片 | 亚洲国产日韩一区无码精品久久久久 | 国产又粗又硬又大色情免费看 | 日操夜操| 清纯唯美亚洲综合 | 久久国产精品免费热7788 |