Leetcode: Expression Add Operators
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. Examples: "123", 6 -> ["1+2+3", "1*2*3"] "232", 8 -> ["2*3+2", "2+3*2"] "105", 5 -> ["1*0+5","10-5"] "00", 0 -> ["0+0", "0-0", "0*0"] "3456237490", 9191 -> []
参考http://segmentfault.com/a/1190000003797204 and http://blog.csdn.net/u013027996/article/details/48713751
深度优先搜索
复杂度
时间 O(N^2) 空间 O(N)
思路
因为要输出所有可能的情况,必定是用深度优先搜索。问题在于如何将问题拆分成多次搜索。加减法很好处理,每当我们截出一段数字时,将之前计算的结果加上或者减去这个数,就可以将剩余的数字字符串和新的计算结果代入下一次搜索中了,直到我们的计算结果和目标一样,就完成了一次搜索。然而,乘法如何处理呢?这里我们需要用一个变量记录乘法当前累乘的值,直到累乘完了,遇到下一个加号或减号再将其算入计算结果中。这里有两种情况:
-
乘号之前是加号或减号,例如
2+3*4
,我们在2那里算出来的结果,到3的时候会加上3,计算结果变为5。在到4的时候,因为4之前我们选择的是乘号,这里3就应该和4相乘,而不是和2相加,所以在计算结果时,要将5先减去刚才加的3得到2,然后再加上3乘以4,得到2+12=14
,这样14就是到4为止时的计算结果。 -
另外一种情况是乘号之前也是乘号,如果
2+3*4*5
,这里我们到4为止计算的结果是14了,然后我们到5的时候又是乘号,这时候我们要把刚才加的3*4给去掉,然后再加上3*4*5
,也就是14-3*4+3*4*5=62
。这样5的计算结果就是62。所以preVal = preVal*curVal
因为要解决上述几种情况,我们需要这么几个变量,一个是记录上次的计算结果currRes
,一个是记录上次被加或者被减的数prevNum
,一个是当前准备处理的数currNum
。当下一轮搜索是加减法时,prevNum
就是简单换成currNum
,当下一轮搜索是乘法时,prevNum
是prevNum
乘以currNum
。
注意
-
第一次搜索不添加运算符,只添加数字,就不会出现
+1+2
这种表达式了。 -
我们截出的数字不能包含0001这种前面有0的数字,但是一个0是可以的。这里一旦截出的数字前导为0,就可以return了,因为说明前面就截的不对,从这之后都是开始为0的,后面也不可能了。
1 public class Solution { 2 public List<String> addOperators(String num, int target) { 3 List<String> res = new ArrayList<String>(); 4 if (num==null || num.length()==0) return res; 5 helper(res, "", num, 0, 0, 0, (long)target); 6 return res; 7 } 8 9 public void helper(List<String> res, String path, String num, int pos, long sum, long preVal, long target) { 10 if (pos == num.length()) { 11 if (sum == target) { 12 res.add(path); 13 } 14 return; 15 } 16 for (int i=pos+1; i<=num.length(); i++) { 17 if (num.charAt(pos)=='0' && i>pos+1) break; // case like: 105 1+05 wrong, 1+0+5 right 18 String curStr = num.substring(pos, i); 19 long cur = Long.parseLong(curStr); 20 if (pos == 0) { 21 helper(res, curStr, num, i, cur, cur, target); 22 } 23 else { 24 // '+' 25 helper(res, path+"+"+curStr, num, i, sum+cur, cur, target); 26 // '-' 27 helper(res, path+"-"+curStr, num, i, sum-cur, -cur, target); 28 // '*' 29 helper(res, path+"*"+curStr, num, i, sum-preVal+preVal*cur, preVal*cur, target); 30 } 31 } 32 } 33 }
相关文章
- Leetcode: The Maze(Unsolved locked problem)
- Leetcode: Integer Replacement
- Leetcode: Maximum Product Subarray
- Leetcode: Path Sum II
- Leetcode: Add Binary
- Leetcode: Merge Two Sorted Lists
- 【Leetcode】70. 爬楼梯
- 【Leetcode】100. 相同的树(简单)
- LeetCode 394. 字符串解码
- 【LeetCode】241. Different Ways to Add Parentheses
- 【LeetCode】95. Unique Binary Search Trees II
- 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)
- 【LeetCode】134. Gas Station
- [Leetcode]-Merge Two Sorted Lists
- leetcode 67 Add Binary
- 【Leetcode】28: 实现 strStr()
- LeetCode 2.两数相加
- [LeetCode] Self Dividing Numbers 自整除数字
- [LeetCode] 623. Add One Row to Tree 二叉树中增加一行
- [LeetCode] Add Two Numbers II 两个数字相加之二
- [LeetCode] 244. Shortest Word Distance II 最短单词距离之二
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
- Leetcode——2. Add Two Numbers
- leetcode算法13.罗马数字转整数