<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)254:因子的組合

          共 3484字,需瀏覽 7分鐘

           ·

          2021-05-05 06:59

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(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.

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

          示例


          注意:
          你可以假定 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整除就進下一個遞歸,當(dāng)start超過sqrt(n)時,start變成n,進下一個遞歸。

          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);
              }
          }


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

          上期推文:

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

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

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

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

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

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

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

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

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

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

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

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

          LeetCode刷題實戰(zhàn)252:會議室

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


          瀏覽 91
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  黄色视频网站在线观看 | 亚洲无码精品久久 | 超哥约草大二91国产最新 | 就要操妓女网 | 日逼免费视频网站 |