Skip to content

Commit 357cc11

Browse files
author
liwentian
committed
fd
1 parent a9e2364 commit 357cc11

File tree

7 files changed

+69
-4
lines changed

7 files changed

+69
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
|251|[Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/Vector2D.java)|100|
206206
|252|[Meeting Rooms](https://leetcode.com/problems/meeting-rooms/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MeetingRooms.java)|100|
207207
|253|[Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MeetingRoomsII.java)|90|
208+
|254|[Factor Combinations](https://leetcode.com/problems/factor-combinations/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FactorCombinations.java)|80||
208209
|255|[Verify Preorder Sequence in Binary Search Tree](https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/VerifyPreorderSequenceInBinarySearchTree.java)|85|很简单,粗心错了|
209210
|256|[Paint House](https://leetcode.com/problems/paint-house/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/PaintHouse.java)|95|
210211
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/BinaryTreePaths.java)|80|

ebook/Backtracking.tex

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,28 @@ \subsubsection{Descriptor}
20372037

20382038
\subsubsection{Solution}
20392039
\begin{Code}
2040+
public List<List<Integer>> getFactors(int n) {
2041+
List<List<Integer>> result = new LinkedList<>();
2042+
dfs(n, 2, result, new LinkedList<>());
2043+
return result;
2044+
}
2045+
2046+
private void dfs(int n, int cur, List<List<Integer>> result, List<Integer> list) {
2047+
if (n <= 1) {
2048+
if (list.size() > 1) {
2049+
result.add(new LinkedList<>(list));
2050+
}
2051+
return;
2052+
}
20402053

2054+
for (int i = cur; i <= n; i++) {
2055+
if (n % i == 0) {
2056+
list.add(i);
2057+
dfs(n / i, i, result, list);
2058+
list.remove(list.size() - 1);
2059+
}
2060+
}
2061+
}
20412062
\end{Code}
20422063

20432064
\newpage

ebook/leetcode.log

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 6 SEP 2017 09:52
1+
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 6 SEP 2017 10:26
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.
@@ -1969,7 +1969,7 @@ File: images/watch.jpg Graphic file (type QTm)
19691969
.
19701970
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}
19711971

1972-
l.2285 \end{Code}
1972+
l.2306 \end{Code}
19731973

19741974
This error message was generated by an \errmessage
19751975
command, so I can't give any explicit help.
@@ -1982,7 +1982,7 @@ and deduce the truth by order and method.
19821982
.
19831983
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}
19841984

1985-
l.2319 \end{Code}
1985+
l.2340 \end{Code}
19861986

19871987
(That was another \errmessage.)
19881988

@@ -1994,7 +1994,7 @@ File: images/unlock.png Graphic file (type QTm)
19941994
.
19951995
\FV@Error ...ncyVerb Error:^^J\space \space #1^^J}
19961996

1997-
l.2419 \end{Code}
1997+
l.2440 \end{Code}
19981998

19991999
(That was another \errmessage.)
20002000

ebook/leetcode.pdf

407 Bytes
Binary file not shown.

ebook/leetcode.synctex.gz

1.41 KB
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.inuker.solution;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by liwentian on 2017/9/6.
8+
*/
9+
10+
public class FactorCombinations {
11+
12+
public List<List<Integer>> getFactors(int n) {
13+
List<List<Integer>> result = new LinkedList<>();
14+
dfs(n, 2, result, new LinkedList<>());
15+
return result;
16+
}
17+
18+
private void dfs(int n, int cur, List<List<Integer>> result, List<Integer> list) {
19+
if (n <= 1) {
20+
if (list.size() > 1) {
21+
result.add(new LinkedList<>(list));
22+
}
23+
return;
24+
}
25+
26+
for (int i = cur; i <= n; i++) {
27+
if (n % i == 0) {
28+
list.add(i);
29+
dfs(n / i, i, result, list);
30+
list.remove(list.size() - 1);
31+
}
32+
}
33+
}
34+
}

test/src/main/java/com/example/Main.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example;
22

3+
import com.inuker.solution.FactorCombinations;
34
import com.inuker.solution.RestoreIPAddresses;
45
import com.leetcode.google.DecodeString;
56
import com.leetcode.google.GenerateParentheses;
@@ -27,6 +28,14 @@
2728
public class Main {
2829

2930
public static void main(String[] args) {
31+
List<List<Integer>> lists = new FactorCombinations().getFactors(128);
32+
// List<List<Integer>> lists = new FactorCombinations().getFactors(23848713);
33+
for (List<Integer> list : lists) {
34+
for (Integer n : list) {
35+
System.out.print(n + " ");
36+
}
37+
System.out.println();
38+
}
3039
}
3140
}
3241

0 commit comments

Comments
 (0)