File tree 6 files changed +281
-2
lines changed
solution/0200-0299/0254.Factor Combinations
6 files changed +281
-2
lines changed Original file line number Diff line number Diff line change 61
61
62
62
<!-- 这里可写通用的实现逻辑 -->
63
63
64
+ ** 方法一:回溯**
65
+
66
+ 我们设计函数 $dfs(n, i)$,其中 $n$ 表示当前待分解的数,$i$ 表示当前分解的数的最大因子,函数的作用是将 $n$ 分解为若干个因子,其中每个因子都不小于 $i$,并将所有分解结果保存到 $ans$ 中。
67
+
68
+ 在函数 $dfs(n, i)$ 中,我们从 $i$ 开始枚举 $n$ 的因子 $j$,如果 $j$ 是 $n$ 的因子,那么我们将 $j$ 加入当前分解结果,然后继续分解 $n / j$,即调用函数 $dfs(n / j, j)$。
69
+
70
+ 时间复杂度 $O(\sqrt{n})$。
71
+
64
72
<!-- tabs:start -->
65
73
66
74
### ** Python3**
67
75
68
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
77
70
78
``` python
71
-
79
+ class Solution :
80
+ def getFactors (self , n : int ) -> List[List[int ]]:
81
+ def dfs (n , i ):
82
+ if t:
83
+ ans.append(t + [n])
84
+ j = i
85
+ while j * j <= n:
86
+ if n % j == 0 :
87
+ t.append(j)
88
+ dfs(n // j, j)
89
+ t.pop()
90
+ j += 1
91
+ t = []
92
+ ans = []
93
+ dfs(n, 2 )
94
+ return ans
72
95
```
73
96
74
97
### ** Java**
75
98
76
99
<!-- 这里可写当前语言的特殊实现逻辑 -->
77
100
78
101
``` java
102
+ class Solution {
103
+ private List<Integer > t = new ArrayList<> ();
104
+ private List<List<Integer > > ans = new ArrayList<> ();
105
+
106
+ public List<List<Integer > > getFactors (int n ) {
107
+ dfs(n, 2 );
108
+ return ans;
109
+ }
110
+
111
+ private void dfs (int n , int i ) {
112
+ if (! t. isEmpty()) {
113
+ List<Integer > cp = new ArrayList<> (t);
114
+ cp. add(n);
115
+ ans. add(cp);
116
+ }
117
+ for (int j = i; j <= n / j; ++ j) {
118
+ if (n % j == 0 ) {
119
+ t. add(j);
120
+ dfs(n / j, j);
121
+ t. remove(t. size() - 1 );
122
+ }
123
+ }
124
+ }
125
+ }
126
+ ```
127
+
128
+ ### ** C++**
129
+
130
+ ``` cpp
131
+ class Solution {
132
+ public:
133
+ vector<vector<int >> getFactors(int n) {
134
+ vector<int > t;
135
+ vector<vector<int >> ans;
136
+ function<void(int, int)> dfs = [ &] (int n, int i) {
137
+ if (t.size()) {
138
+ vector<int > cp = t;
139
+ cp.emplace_back(n);
140
+ ans.emplace_back(cp);
141
+ }
142
+ for (int j = i; j <= n / j; ++j) {
143
+ if (n % j == 0) {
144
+ t.emplace_back(j);
145
+ dfs(n / j, j);
146
+ t.pop_back();
147
+ }
148
+ }
149
+ };
150
+ dfs(n, 2);
151
+ return ans;
152
+ }
153
+ };
154
+ ```
79
155
156
+ ### **Go**
157
+
158
+ ```go
159
+ func getFactors(n int) [][]int {
160
+ t := []int{}
161
+ ans := [][]int{}
162
+ var dfs func(n, i int)
163
+ dfs = func(n, i int) {
164
+ if len(t) > 0 {
165
+ cp := make([]int, len(t))
166
+ copy(cp, t)
167
+ cp = append(cp, n)
168
+ ans = append(ans, cp)
169
+ }
170
+ for j := i; j <= n/j; j++ {
171
+ if n%j == 0 {
172
+ t = append(t, j)
173
+ dfs(n/j, j)
174
+ t = t[:len(t)-1]
175
+ }
176
+ }
177
+ }
178
+ dfs(n, 2)
179
+ return ans
180
+ }
80
181
```
81
182
82
183
### ** ...**
Original file line number Diff line number Diff line change 50
50
### ** Python3**
51
51
52
52
``` python
53
-
53
+ class Solution :
54
+ def getFactors (self , n : int ) -> List[List[int ]]:
55
+ def dfs (n , i ):
56
+ if t:
57
+ ans.append(t + [n])
58
+ j = i
59
+ while j * j <= n:
60
+ if n % j == 0 :
61
+ t.append(j)
62
+ dfs(n // j, j)
63
+ t.pop()
64
+ j += 1
65
+ t = []
66
+ ans = []
67
+ dfs(n, 2 )
68
+ return ans
54
69
```
55
70
56
71
### ** Java**
57
72
58
73
``` java
74
+ class Solution {
75
+ private List<Integer > t = new ArrayList<> ();
76
+ private List<List<Integer > > ans = new ArrayList<> ();
77
+
78
+ public List<List<Integer > > getFactors (int n ) {
79
+ dfs(n, 2 );
80
+ return ans;
81
+ }
82
+
83
+ private void dfs (int n , int i ) {
84
+ if (! t. isEmpty()) {
85
+ List<Integer > cp = new ArrayList<> (t);
86
+ cp. add(n);
87
+ ans. add(cp);
88
+ }
89
+ for (int j = i; j <= n / j; ++ j) {
90
+ if (n % j == 0 ) {
91
+ t. add(j);
92
+ dfs(n / j, j);
93
+ t. remove(t. size() - 1 );
94
+ }
95
+ }
96
+ }
97
+ }
98
+ ```
99
+
100
+ ### ** C++**
101
+
102
+ ``` cpp
103
+ class Solution {
104
+ public:
105
+ vector<vector<int >> getFactors(int n) {
106
+ vector<int > t;
107
+ vector<vector<int >> ans;
108
+ function<void(int, int)> dfs = [ &] (int n, int i) {
109
+ if (t.size()) {
110
+ vector<int > cp = t;
111
+ cp.emplace_back(n);
112
+ ans.emplace_back(cp);
113
+ }
114
+ for (int j = i; j <= n / j; ++j) {
115
+ if (n % j == 0) {
116
+ t.emplace_back(j);
117
+ dfs(n / j, j);
118
+ t.pop_back();
119
+ }
120
+ }
121
+ };
122
+ dfs(n, 2);
123
+ return ans;
124
+ }
125
+ };
126
+ ```
59
127
128
+ ### **Go**
129
+
130
+ ```go
131
+ func getFactors(n int) [][]int {
132
+ t := []int{}
133
+ ans := [][]int{}
134
+ var dfs func(n, i int)
135
+ dfs = func(n, i int) {
136
+ if len(t) > 0 {
137
+ cp := make([]int, len(t))
138
+ copy(cp, t)
139
+ cp = append(cp, n)
140
+ ans = append(ans, cp)
141
+ }
142
+ for j := i; j <= n/j; j++ {
143
+ if n%j == 0 {
144
+ t = append(t, j)
145
+ dfs(n/j, j)
146
+ t = t[:len(t)-1]
147
+ }
148
+ }
149
+ }
150
+ dfs(n, 2)
151
+ return ans
152
+ }
60
153
```
61
154
62
155
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<vector<int >> getFactors (int n) {
4
+ vector<int > t;
5
+ vector<vector<int >> ans;
6
+ function<void (int , int )> dfs = [&](int n, int i) {
7
+ if (t.size ()) {
8
+ vector<int > cp = t;
9
+ cp.emplace_back (n);
10
+ ans.emplace_back (cp);
11
+ }
12
+ for (int j = i; j <= n / j; ++j) {
13
+ if (n % j == 0 ) {
14
+ t.emplace_back (j);
15
+ dfs (n / j, j);
16
+ t.pop_back ();
17
+ }
18
+ }
19
+ };
20
+ dfs (n, 2 );
21
+ return ans;
22
+ }
23
+ };
Original file line number Diff line number Diff line change
1
+ func getFactors (n int ) [][]int {
2
+ t := []int {}
3
+ ans := [][]int {}
4
+ var dfs func (n , i int )
5
+ dfs = func (n , i int ) {
6
+ if len (t ) > 0 {
7
+ cp := make ([]int , len (t ))
8
+ copy (cp , t )
9
+ cp = append (cp , n )
10
+ ans = append (ans , cp )
11
+ }
12
+ for j := i ; j <= n / j ; j ++ {
13
+ if n % j == 0 {
14
+ t = append (t , j )
15
+ dfs (n / j , j )
16
+ t = t [:len (t )- 1 ]
17
+ }
18
+ }
19
+ }
20
+ dfs (n , 2 )
21
+ return ans
22
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private List <Integer > t = new ArrayList <>();
3
+ private List <List <Integer >> ans = new ArrayList <>();
4
+
5
+ public List <List <Integer >> getFactors (int n ) {
6
+ dfs (n , 2 );
7
+ return ans ;
8
+ }
9
+
10
+ private void dfs (int n , int i ) {
11
+ if (!t .isEmpty ()) {
12
+ List <Integer > cp = new ArrayList <>(t );
13
+ cp .add (n );
14
+ ans .add (cp );
15
+ }
16
+ for (int j = i ; j <= n / j ; ++j ) {
17
+ if (n % j == 0 ) {
18
+ t .add (j );
19
+ dfs (n / j , j );
20
+ t .remove (t .size () - 1 );
21
+ }
22
+ }
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def getFactors (self , n : int ) -> List [List [int ]]:
3
+ def dfs (n , i ):
4
+ if t :
5
+ ans .append (t + [n ])
6
+ j = i
7
+ while j * j <= n :
8
+ if n % j == 0 :
9
+ t .append (j )
10
+ dfs (n // j , j )
11
+ t .pop ()
12
+ j += 1
13
+ t = []
14
+ ans = []
15
+ dfs (n , 2 )
16
+ return ans
You can’t perform that action at this time.
0 commit comments