[Swift]LeetCode254.因子组合 $ 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:

  1. Each combination‘s factors must be sorted ascending, for example: The factors of 2 and 6 is [2, 6], not [6, 2].
  2. You may assume that n is always positive.
  3. 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]
]


数字可以被视为其因子的乘积。例如,

8 = 2 x 2 x 2;
  = 2 x 4.

编写一个接受整数n并返回所有可能的因子组合的函数。

注:

每个组合的因子必须按升序排序,例如:2和6的因子是[2,6],而不是[6,2]。

你可以假设n总是正的。

系数应大于1且小于n。

实例:

输入:1

输出:

[]

输入:37

输出:

[]

输入:12

输出:

[
  [2, 6],
  [2, 2, 3],
  [3, 4]
]

输入:32

输出:

[
  [2, 16],
  [2, 2, 8],
  [2, 2, 2, 4],
  [2, 2, 2, 2, 2],
  [2, 4, 4],
  [4, 8]
]

 1 class Solution {
 2     func getFactors(_ n:Int) -> [[Int]]{
 3         var res:[[Int]] = [[Int]]()
 4         var arr:[Int] = [Int]()
 5         helper(n,2,&arr,&res)
 6         return res
 7     }
 8
 9     func helper(_ n:Int,_ start:Int,_ out:inout [Int],_ res:inout [[Int]])
10     {
11         var num:Int = Int(floor(sqrt(Double(n))))
12         var i :Int = start
13         while(i <= num)
14         {
15             if n % i == 0
16             {
17                 var new_out:[Int] = out
18                 new_out.append(i)
19                 helper(n / i, i, &new_out, &res)
20                 new_out.append(n / i)
21                 res.append(new_out)
22             }
23             i += 1
24         }
25     }
26 }

原文地址:https://www.cnblogs.com/strengthen/p/10225203.html

时间: 2024-10-17 03:42:50

[Swift]LeetCode254.因子组合 $ Factor Combinations的相关文章

Factor Combinations

Factor Combinations Problem: 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 b

[LeetCode] 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: Each combination's factors must be sorted ascending, for examp

[LeetCode#254] Factor Combinations

Problem: 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,

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: Each combination's factors must be sorted ascending, for e

[Locked] 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: Each combination‘s factors must be sorted ascending, for examp

[LC] 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 gr

[LeetCode]Factor Combinations

这个题目比较要注意的一点是如何防止重复.在dfs中,下一层的因子应该大于等于上一层的 public class Solution { List<List<Integer>> result = new ArrayList<List<Integer>>(); public List<List<Integer>> getFactors(int n) { if (n == 1) { return result; } helper(n, new

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.