-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-282-Expression-Add-Operators.java
64 lines (49 loc) · 2.17 KB
/
LeetCode-282-Expression-Add-Operators.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
https://leetcode.com/discuss/58614/java-standard-backtrace-ac-solutoin-short-and-clear
https://leetcode.com/discuss/75308/java-simple-solution-beats-96-56%25
http://www.cnblogs.com/grandyang/p/4814506.html
Test case:
"105" 5 -> ["1*0+5","1*5","10-5"] should be ["1*0+5","10-5"]
*/
public class Solution {
public List<String> addOperators(String num, int target) {
List<String> result = new ArrayList<String>();
if(num == null || num.length() == 0) return result;
StringBuilder sb = new StringBuilder();
DFS(num, target, 0, result, sb, 0, 0);
// dfs(result, sb, num, 0, target, 0, 0);
return result;
}
private void DFS(String num, int target, int start, List<String> result, StringBuilder sb, long val, long multed){
// end condition
if(start == num.length()){
if(val == target){
result.add(sb.toString());
}
return;
}
for(int i = start; i < num.length(); i++){
// "105" 5 -> ["1*0+5","1*5","10-5"] should be ["1*0+5","10-5"]
// Careful: 1*05 this case should be elimited
if(num.charAt(start) == '0' && i != start) break;
long curr = Long.parseLong(num.substring(start, i + 1));
int len = sb.length(); // used to remove new added chars in sb
if(start == 0){
// first num
sb.append(curr);
DFS(num, target, i + 1, result, sb, curr, curr);
sb.setLength(len); //erase the modification
}else{
sb.append("+").append(curr);
DFS(num, target, i + 1, result, sb, val + curr, curr);
sb.setLength(len);
sb.append("-").append(curr);
DFS(num, target, i + 1, result, sb, val - curr, -curr);
sb.setLength(len);
sb.append("*").append(curr);
DFS(num, target, i + 1, result, sb, val - multed + multed * curr, multed * curr);
sb.setLength(len);
}
}
}
}