日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

254. Factor Combinations

發(fā)布時(shí)間:2024/4/14 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 254. Factor Combinations 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:

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.

Note:?

  • Each combination's factors must be sorted ascending, for example: The factors of 2 and 6 is?[2, 6], not?[6, 2].
  • You may assume that?n?is always positive.
  • Factors should be greater than 1 and less than?n.
  • ?

    Examples:?
    input:?1
    output:?

    [] input:?37
    output:?
    [] input:?12
    output:
    [[2, 6],[2, 2, 3],[3, 4] ] input:?32
    output:
    [[2, 16],[2, 2, 8],[2, 2, 2, 4],[2, 2, 2, 2, 2],[2, 4, 4],[4, 8] ]

    鏈接:?http://leetcode.com/problems/factor-combinations/

    題解:

    求一個(gè)數(shù)的所有factor,這里我們又想到了DFS + Backtracking, 需要注意的是,factor都是>= 2的,并且在此題里,這個(gè)數(shù)本身不能算作factor,所以我們有了當(dāng)n <= 1時(shí)的判斷 if(list.size() > 1) add the result to res.

    Time Complexity - O(2n), Space Complexity - O(n).

    public class Solution {public List<List<Integer>> getFactors(int n) {List<List<Integer>> res = new ArrayList<>();List<Integer> list = new ArrayList<>();getFactors(res, list, n, 2);return res;}private void getFactors(List<List<Integer>> res, List<Integer> list, int n, int factor) {if(n <= 1) {if(list.size() > 1)res.add(new ArrayList<Integer>(list));return;}for(int i = factor; i <= n; i++) {if(n % i == 0) {list.add(i);getFactors(res, list, n / i, i);list.remove(list.size() - 1);}}} }

    ?

    二刷:

    還是使用了一刷的辦法,dfs + backtracking。但遞歸結(jié)束的條件更新成了n == 1。 但是速度并不是很快,原因是沒有做剪枝。我們其實(shí)可以設(shè)置一個(gè)upper limit,即當(dāng)i > Math.sqrt(n)的時(shí)候,我們不能繼續(xù)進(jìn)行下一輪遞歸,此時(shí)就要跳出了。

    Java:

    public class Solution {public List<List<Integer>> getFactors(int n) {List<List<Integer>> res = new ArrayList<>();if (n <= 1) return res;getFactors(res, new ArrayList<>(), n, 2);return res;}private void getFactors(List<List<Integer>> res, List<Integer> list, int n, int pos) {if (n == 1) {if (list.size() > 1) res.add(new ArrayList<>(list));return;}for (int i = pos; i <= n; i++) {if (n % i == 0) {list.add(i);getFactors(res, list, n / i, i);list.remove(list.size() - 1);}}} }

    ?

    Update: 使用@yuhangjiang的方法,只用計(jì)算 2到sqrt(n)的這么多因子,大大提高了速度。

    public class Solution {public List<List<Integer>> getFactors(int n) {List<List<Integer>> res = new ArrayList<>();if (n <= 1) return res;getFactors(res, new ArrayList<>(), n, 2);return res;}private void getFactors(List<List<Integer>> res, List<Integer> list, int n, int pos) {for (int i = pos; i <= Math.sqrt(n); i++) {if (n % i == 0 && n / i >= i) {list.add(i);list.add(n / i);res.add(new ArrayList<>(list));list.remove(list.size() - 1);getFactors(res, list, n / i, i);list.remove(list.size() - 1);}}} }

    ?

    ?

    ?

    ?

    Reference:

    https://leetcode.com/discuss/51261/iterative-and-recursive-python

    https://leetcode.com/discuss/87926/java-2ms-easy-to-understand-short-and-sweet

    https://leetcode.com/discuss/58828/a-simple-java-solution

    https://leetcode.com/discuss/72224/my-short-java-solution-which-is-easy-to-understand?

    https://leetcode.com/discuss/82087/share-bit-the-thought-process-short-java-bottom-and-top-down

    總結(jié)

    以上是生活随笔為你收集整理的254. Factor Combinations的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。