Factor Combinations
Description
null
Solution(javascript)
// /** 1: 手动去重的版本
// * @param {number} n
// * @return {number[][]}
// */
// const getFactors = function (n) {
// const result = {}
// const aux = (remain, current = []) => {
// if (remain === 1) {
// if (current.length > 1) {
// current.sort((a, b) => a - b)
// const key = current.join('')
// result[key] = current
// }
// return
// }
// for (let i = 2; i <= remain; i++) {
// if (remain % i === 0) {
// aux(remain / i, [...current, i])
// }
// }
// }
// aux(n)
// return Object.values(result)
// }
/** 2: 控制边界去重
* @param {number} n
* @return {number[][]}
*/
const getFactors = function (n) {
const result = []
const aux = (remain, start = 2, current = []) => {
if (remain === 1) {
if (current.length > 1) {
result.push([...current])
}
return
}
for (let i = start; i <= remain; i++) {
if (remain % i === 0) {
current.push(i)
aux(remain / i, i, current)
current.pop()
}
}
}
aux(n)
return result
}