Factor Combination-质数分解

1. Link

2. 题目

Given an integer number, return all possible combinations of the factors that can multiply to the target number.

Example

Give A = 24

since 24 = 2 x 2 x 2 x 3

= 2 x 2 x 6

= 2 x 3 x 4

= 2 x 12

= 3 x 8

= 4 x 6

your solution should return

{ { 2, 2, 2, 3 }, { 2, 2, 6 }, { 2, 3, 4 }, { 2, 12 }, { 3, 8 }, { 4, 6 } }

note: duplicate combination is not allowed.

3. 思路

4. Coding

如果只考虑所有质数,那就不用for loop直接看target能不能被start整除(之后start的倍数都会被直接跳过,因为target都已经不能被start整除那就更不可能被它的倍数整除)。如果可以就加上start,如果不行就跳过当前的start,进入下一个factor: start+1

Last updated

Was this helpful?