Skip to content

Commit 00706a3

Browse files
refactor 282
1 parent b070102 commit 00706a3

File tree

1 file changed

+37
-31
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+37
-31
lines changed

src/main/java/com/fishercoder/solutions/_282.java

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import java.util.List;
55

66
/**
7-
* Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value.
7+
* 282. Expression Add Operators
8+
*
9+
* Given a string that contains only digits 0-9 and a target value,
10+
* return all possibilities to add binary operators (not unary) +, -, or * between the digits
11+
* so they evaluate to the target value.
812
913
Examples:
1014
"123", 6 -> ["1+2+3", "1*2*3"]
@@ -15,39 +19,41 @@
1519
*/
1620
public class _282 {
1721

18-
public List<String> addOperators(String num, int target) {
19-
List<String> res = new ArrayList<String>();
20-
StringBuilder sb = new StringBuilder();
21-
dfs(res, sb, num, 0, target, 0, 0);
22-
return res;
22+
public static class Solution1 {
23+
public List<String> addOperators(String num, int target) {
24+
List<String> res = new ArrayList<String>();
25+
StringBuilder sb = new StringBuilder();
26+
dfs(res, sb, num, 0, target, 0, 0);
27+
return res;
2328

24-
}
25-
26-
private void dfs(List<String> res, StringBuilder sb, String num, int pos, int target, long prev, long multi) {
27-
if (pos == num.length()) {
28-
if (target == prev) {
29-
res.add(sb.toString());
30-
}
31-
return;
3229
}
33-
for (int i = pos; i < num.length(); i++) {
34-
if (num.charAt(pos) == '0' && i != pos) {
35-
break;
30+
31+
private void dfs(List<String> res, StringBuilder sb, String num, int pos, int target, long prev, long multi) {
32+
if (pos == num.length()) {
33+
if (target == prev) {
34+
res.add(sb.toString());
35+
}
36+
return;
3637
}
37-
long curr = Long.parseLong(num.substring(pos, i + 1));
38-
int len = sb.length();
39-
if (pos == 0) {
40-
dfs(res, sb.append(curr), num, i + 1, target, curr, curr);
41-
sb.setLength(len);
42-
} else {
43-
dfs(res, sb.append("+").append(curr), num, i + 1, target, prev + curr, curr);
44-
sb.setLength(len);
45-
46-
dfs(res, sb.append("-").append(curr), num, i + 1, target, prev - curr, -curr);
47-
sb.setLength(len);
48-
49-
dfs(res, sb.append("*").append(curr), num, i + 1, target, prev - multi + multi * curr, multi * curr);
50-
sb.setLength(len);
38+
for (int i = pos; i < num.length(); i++) {
39+
if (num.charAt(pos) == '0' && i != pos) {
40+
break;
41+
}
42+
long curr = Long.parseLong(num.substring(pos, i + 1));
43+
int len = sb.length();
44+
if (pos == 0) {
45+
dfs(res, sb.append(curr), num, i + 1, target, curr, curr);
46+
sb.setLength(len);
47+
} else {
48+
dfs(res, sb.append("+").append(curr), num, i + 1, target, prev + curr, curr);
49+
sb.setLength(len);
50+
51+
dfs(res, sb.append("-").append(curr), num, i + 1, target, prev - curr, -curr);
52+
sb.setLength(len);
53+
54+
dfs(res, sb.append("*").append(curr), num, i + 1, target, prev - multi + multi * curr, multi * curr);
55+
sb.setLength(len);
56+
}
5157
}
5258
}
5359
}

0 commit comments

Comments
 (0)