254. 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.

Note:
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://www.reibang.com/p/883fdda93a66

Solution:Backtracking

實現(xiàn)1a: regular
實現(xiàn)1b: i <= (int) Math.sqrt(n) instead of n, to speed up. some small changes required for corner cases.

Time Complexity: T(N) = T(N/2) + T(N/3) + T(N/4) + .. ? Space Complexity: O(N)

Solution1a Code:

class Solution {
    public List<List<Integer>> getFactors(int n) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        List<Integer> cur_res = new ArrayList<>();
        
        getFactors(n, 2, cur_res, result);
        
        return result;
    }

    public void getFactors(int n, int start, List<Integer> cur_res, List<List<Integer>> result){
        if (n == 1 && cur_res.size() > 1) { // cur_res.size() > 1: to avoid the n itself to be the factor
            result.add(new ArrayList<Integer>(cur_res));
            return;
        }
        
        for (int i = start; i <= n; ++i) {
            if (n % i == 0) {
                cur_res.add(i);
                getFactors(n / i, i, cur_res, result);
                cur_res.remove(cur_res.size() - 1);
            }
        }
    }
}

Solution1b Code:

class Solution {
    public List<List<Integer>> getFactors(int n) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        List<Integer> cur_res = new ArrayList<>();
        
        getFactors(n, 2, cur_res, result);
        
        return result;
    }

    public void getFactors(int n, int start, List<Integer> cur_res, List<List<Integer>> result){
        if (n == 1 ) {
            if(cur_res.size() > 1) { // cur_res.size() > 1: to avoid the n itself to be the factor
                result.add(new ArrayList<Integer>(cur_res));
            }
            
            return;
        }
            

        for (int i = start; i <= (int) Math.sqrt(n); ++i) { // ===> here, change 1
            if (n % i == 0) {
                cur_res.add(i);
                getFactors(n / i, i, cur_res, result);
                cur_res.remove(cur_res.size() - 1);
            }
        }
        
        int i = n; // ===> here, change 2, try to 處理 (int) Math.sqrt(n) 達不到原數(shù)的情況,如根2 = 1,則達不到2尤筐,這里corner case 加上
        cur_res.add(i);
        getFactors(n / i, i, cur_res, result);
        cur_res.remove(cur_res.size() - 1);
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市宦芦,隨后出現(xiàn)的幾起案子橱鹏,更是在濱河造成了極大的恐慌葛假,老刑警劉巖情竹,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件藐不,死亡現(xiàn)場離奇詭異,居然都是意外死亡秦效,警方通過查閱死者的電腦和手機雏蛮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來棉安,“玉大人底扳,你說我怎么就攤上這事」钡ⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蒲赂。 經常有香客問我阱冶,道長,這世上最難降的妖魔是什么滥嘴? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任木蹬,我火速辦了婚禮,結果婚禮上若皱,老公的妹妹穿的比我還像新娘镊叁。我一直安慰自己,他們只是感情好走触,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布晦譬。 她就那樣靜靜地躺著,像睡著了一般互广。 火紅的嫁衣襯著肌膚如雪敛腌。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天惫皱,我揣著相機與錄音像樊,去河邊找鬼。 笑死旅敷,一個胖子當著我的面吹牛生棍,可吹牛的內容都是我干的。 我是一名探鬼主播媳谁,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涂滴,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了韩脑?” 一聲冷哼從身側響起氢妈,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎段多,沒想到半個月后首量,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡进苍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年加缘,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片觉啊。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡拣宏,死狀恐怖,靈堂內的尸體忽然破棺而出杠人,到底是詐尸還是另有隱情勋乾,我是刑警寧澤宋下,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站辑莫,受9級特大地震影響学歧,放射性物質發(fā)生泄漏。R本人自食惡果不足惜各吨,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一枝笨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧揭蜒,春花似錦横浑、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至偶垮,卻和暖如春张咳,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背似舵。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工脚猾, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人砚哗。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓龙助,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蛛芥。 傳聞我的和親對象是個殘疾皇子提鸟,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內容