原題鏈接
題目原文:
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.
Example?1: ?Input:1Output:[]
Example?2: ?Input:37Output:[]
Example?3: ?Input:12Output:[? [2, 6],? [2, 2, 3],? [3, 4]]
Example?4: ?Input:32Output:[? [2, 16],? [2, 2, 8],? [2, 2, 2, 4],? [2, 2, 2, 2, 2],? [2, 4, 4],? [4, 8]]
中文翻譯:
數(shù)字可以被看作是它的約數(shù)的乘積。舉個(gè)例子:
8 = 2 x 2 x 2;
? = 2 x 4.
寫一個(gè)函數(shù),帶一個(gè)整形參數(shù)n, 返回所有可能的約數(shù)組合刻炒。
注意:
你可以假設(shè)n總是正的翘簇。約數(shù)應(yīng)該大于1并且小于n。
例子自己看上面的吧,以后這部分沒(méi)有必要的話,盡量就不翻譯了。
其實(shí)從Test case里分苇,我們已經(jīng)能看出一些解答的端倪,
首先屁桑,每個(gè)解答里面的約數(shù)都介于2~n - 1, 并且是按順序從左到右遞增医寿。
那么思路就是對(duì)于數(shù)字n, 并且把當(dāng)前解答里最后一個(gè)元素的值設(shè)為bound, n則需要找到 bound ~ sqrt (n)范圍內(nèi)的約數(shù)。這里sqrt(n)則是n的開根號(hào)蘑斧,因?yàn)閿?shù)字是遞增的缭受,所以不能夠允許剩余的數(shù)字小于bound